【问题标题】:how to compare current time with two different times along with dayofweek in unix shell script?如何在 unix shell 脚本中将当前时间与两个不同的时间以及 dayofweek 进行比较?
【发布时间】:2017-04-27 13:50:20
【问题描述】:

如何比较两个时间戳和另一个条件。 请找到以下代码作为解决方法的试用版。

d=$(date +%Y%m%d)          #Today
d1=$(date +%b" "%d)        #Centre1 col 1 & 2 (MON DD)
ct=$(date +'%H%M%S')       #Current Time (HHMM)
t01='013000'
t02='033000'
t03='053000'
t04='073000'


find . -mtime 0 -iname "RBDEXT*.csv" -ls | awk '{printf("%-5s%s\t%-40s%s\t%s\t\n", $8,$9,$11,$10,$7)}' > rbdextmp1.txt


rbdextCO=$(wc -l rbdextmp1.txt | awk '{print $1}')
rbdextIN=$(cat rbdextmp1.txt | grep "inprogress" | wc -l)

touch centre.txt

if [[ [ "$rbdextIN" -eq 0 ] &&
 [ [ "$ct" -gt "$t01" ] && [ "$ct" -lt "$t02" ] && [ "$rbdextCO" -eq 1 ]  ||
   [ "$ct" -gt "$t02" ] && [ "$ct" -lt "$t03" ] && [ "$rbdextCO" -eq 2 ]  ||
   [ "$ct" -gt "$t03" ] && [ "$ct" -lt "$t04" ] && [ "$rbdextCO" -eq 3 ] ]
 ]]

then
echo "$d1 RBDEXT.$d.csv($rbdextCO) OK" >> centre.txt
elif [ "$rbdextIN" -ge 1 ]
then
echo "$d1 RBDEXT.$d.csv($rbdextCO) OKBUT" >> centre.txt
else
echo "$d1 RBDEXT.$d.csv($rbdextCO) NOK" >> centre.txt
fi

请您帮我解决这个问题,非常感谢!

【问题讨论】:

  • 不要说“它不适合我”,而是告诉我们它做了什么,你没想到。另外,闭幕式fi在哪里?
  • @EdMorton,先生您好,我根据要求更新了问题。请更新。
  • @EdMorton,先生,我已经更新了这个问题,我想这可能会有所帮助。

标签: shell date unix awk sed


【解决方案1】:

我建议你慢慢来。

开始于:

if [ "2" -gt "1" ]
then
   echo "Green"
else
   echo "Red"
fi

...检查它是否有效。 然后使用&& 添加第二个子句,解决任何语法问题,确保它有效。 然后用变量替换您的硬编码值。 然后使用来自date 的输出填充变量。在每一步之后检查它是否仍然有效。你会到达那里的。

额外提示 - 用于命令替换的反引号已经有一段时间不受欢迎了,因为它很容易出错。请改用currentTime=$(date +%H%M%S)

【讨论】:

    【解决方案2】:

    听起来这就是你想要的:

    currenttime=$(date +'%H%M%S')
    dayofweek=$(date +'%u')
    
    time1='013000'
    time2='033000'
    time3='053000'
    time4='073000'
    
    count=$(wc -l < xxx.txt)
    
    if (( (dayofweek == 1) &&
          ( ( (currenttime > time1) && (currenttime < time2) && (count == 1) ) ||
            ( (currenttime > time2) && (currenttime < time3) && (count == 2) ) ||
            ( (currenttime > time3) && (currenttime < time4) && (count == 3) ) )
       ))
    then
        color="GREEN"
    else
        color="RED"
    fi
    
    printf 'GP_GLOBAL_FEED(%s)  %s\n' "$count" "$color" |
    mailx -s "$color" abcd@mail.com
    

    【讨论】:

    • 不客气。如果您不确切地告诉我它们是什么,我无法帮助您调试任何错误。
    • ,先生,非常感谢您的输入,我得到了带有以下消息的 else 部分输出,我将整个代码粘贴到了所问的问题中。 ./t.ksh[38]: rbdextIN: not found [No such file or directory]
    • 您发布的产生语法错误的脚本与我发布的脚本不同。例如,您已将 (( ... )) 算术运算分隔符更改为嵌套单括号 (&lt;space&gt;( .. )&lt;space&gt;)。把它放回我给你的风格/语法,然后运行它以确保它有效。现在进行 1 处更改并再次运行它 - 如果您收到错误,则该更改会引入错误。重复,直到您确定导致错误的更改。
    • 不客气。请恢复您的问题。这个网站不仅仅是一个提问的地方,它还是一个知识库,有类似问题的人可以在其中查找这些问题并找到相关的答案。
    • 问题已恢复!谢谢你的时间
    猜你喜欢
    • 1970-01-01
    • 2014-05-12
    • 2013-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-03
    • 1970-01-01
    相关资源
    最近更新 更多