检查当前时间是否在UNIX上定义的时间范围内

检查当前时间是否在UNIX上定义的时间范围内,unix,solaris,ksh,scheduler,systemtime,Unix,Solaris,Ksh,Scheduler,Systemtime,考虑以下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&a

考虑以下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脚本新手,非常感谢您的帮助。

返回命令用于返回退出状态,而不是任意字符串。这与许多其他语言不同。您可以使用
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
start=13:56
完15时05分
checkIfWithinRange(){
当前=$(日期+%H:%M)#获取05:18格式的当前时间
[[($start=$current | |$start<$current)&&($current=$end | |$current<$end)]]
}
如果在范围内进行检查;然后
做点什么
fi

这是什么语言或环境?标签上写着
ksh
,但这看起来不像是代码。我想进一步说明一下,Unix是一种操作系统(操作系统家族),而不是一种编程语言。如何与日期交互更多地取决于编程语言而不是操作系统。Unix上的两个标准编程环境是shell和C,但您的示例代码两者都不是。是的,您必须存储开始时间和结束时间,并将它们与当前时间进行比较。你如何做到这一点取决于你所使用的语言和环境;C中的结构、shell中的变量或文件、Java中的对象、数据库中的列。@BrianCampbell谢谢!我是UNIX korn shell的新手,编写了希望在korn shell中实现的示例伪代码。我将在自己尝试后几分钟内更新问题。谢谢@glenn的详细解释。我会从我这边确认密码然后回来。。