几个月前,我遇到了操作系统管道工的沉思。真正引起我注意的一篇文章是使用类型创建面向对象的 Korn Shell 93 脚本。
从那时起,我一直在网上搜索更多信息/示例。在这段时间里,我得出的结论是它们非常罕见,因为我只找到了 3 个示例和 3 个文档(其中之一是 ksh93 手册页中的“类型变量”)。
如果这里有人知道我可以在哪里找到有关此功能的更多信息,请您将其传递给我。我的计划是就此写一篇文章并将其发布在这里。我知道至少有一个人会觉得它很有帮助。
我知道一点。我会给你这个例子,稍后我会解释我正在做的事情。
#!/usr/local/bin/ksh
# This will do a show and tell using the typeset -T feature
# of ksh
# Sat Mar 30 01:01:35 AM EDT 2024
#
typeset -T TheTime_T=(
typeset -S skew=0
function get {
now=$( date +%s )
(( .sh.value=now+skew ))
(( skew+=1 ))
}
)
typeset -T Upper_T=(
TheTime_T now
typeset one=11
typeset two=2U
typeset countU=0
typeset start="Upper"
function initialize {
typeset -S countS=0 # static
typeset countI=0 # instance
(( _.countU+=1 ))
(( countS+=1 ))
(( countI+=1 ))
echo "init of Upper: ${!_} S=${countS} I=${countI} U=${_.countU}"
}
function setStart {
echo "Upper:setStart ${_.now} $@"
}
function endStart {
echo "Upper:endStart ${_.now} $@"
}
)
typeset -T Middle_T=(
Upper_T _
typeset middleVal="middle value"
typeset start=middle
typeset two="middle"
function initialize {
echo "init of Middle: ${!_}"
.sh.type.Upper_T.initialize ${!_}
}
function endStart {
echo "Middle:endStart $@"
_.two="midEnd"
}
)
typeset -T Lower_T=(
Middle_T _
typeset one=1L
typeset start="lower"
function initialize {
echo "init of Lower: ${!_}"
.sh.type.Upper_T.initialize ${!_}
}
function endStart {
echo "Lower:endStart $@"
echo "Ending the start process in mv=${_.middleVal} t=${_.two} ${_.one}"
}
)
Upper_T uu
uu.initialize toStart
uu.setStart hownow
uu.endStart then
Middle_T mm
mm.initialize inMiddle
mm.setStart middleStart
mm.endStart middleStartThen
当使用 ksh2020 运行时(比我记忆中的要好),结果是:
$ ./tryT.sh
init of Upper: uu S=1 I=0 U=1
Upper:setStart 1711774788 hownow
Upper:endStart 1711774789 then
init of Middle: mm
init of Upper: Upper_T S=2 I=0 U=1
Upper:setStart 1711774790 middleStart
Middle:endStart middleStartThen
init of Lower: ll
init of Upper: Upper_T S=3 I=0 U=2
Upper:setStart 1711774791 startingLower
Lower:endStart endingLower
Ending the start process in mv=lower val t=midEnd lower
我认为手册页中解释了
Name_T _
表示继承。类名以大写字母开头,以 _T
结尾。你不必这样做,但这是一个很好的约定。以 _.
为前缀的变量是类变量。