考虑以下 PSUEDO-CODE :
#!/bin/ksh
rangeStartTime_hr=13
rangeStartTime_min=56
rangeEndTime_hr=15
rangeEndTime_min=05
getCurrentMinute() {
return `date +%M | sed -e 's/0*//'`;
# Used sed to remove the padded 0 on the left. On successfully find&replacing
# the first match it returns the resultant string.
# date command does not provide minutes in long integer format, on Solaris.
}
getCurrentHour() {
return `date +%l`; # %l hour ( 1..12)
}
checkIfWithinRange() {
if [[ getCurrentHour -ge $rangeStartTime_hr &&
getCurrentMinute -ge $rangeStartTime_min ]]; then
# Ahead of start time.
if [[ getCurrentHour -le $rangeEndTime_hr &&
getCurrentMinute -le $rangeEndTime_min]]; then
# Within the time range.
return 0;
else
return 1;
fi
else
return 1;
fi
}
是否有更好的方法来实施checkIfWithinRange()
? UNIX中是否有任何内置函数可以更容易地执行上述操作?我是korn脚本的新手,非常感谢你的投入。
答案 0 :(得分:2)
return
命令用于返回退出状态,而不是任意字符串。这与许多其他语言不同。您使用stdout
传递数据:
getCurrentMinute() {
date +%M | sed -e 's/^0//'
# make sure sed only removes zero from the beginning of the line
# in the case of "00" don't be too greedy so only remove one 0
}
此外,您需要更多语法来调用该函数。目前,您正在比较if条件
中的文字字符串"getCurrentMinute"
if [[ $(getCurrentMinute) -ge $rangeStartTime_min && ...
如果有点不同我会这样做
start=13:56
end=15:05
checkIfWithinRange() {
current=$(date +%H:%M) # Get's the current time in the format 05:18
[[ ($start = $current || $start < $current) && ($current = $end || $current < $end) ]]
}
if checkIfWithinRange; then
do something
fi