如何在unix shell脚本中将当前时间与dayofweek的两个不同时间进行比较?

时间:2016-12-12 16:26:20

标签: shell date unix awk sed

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

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

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

2 个答案:

答案 0 :(得分:2)

我建议你慢慢建立这个。

开始于:

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

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

奖金提示 - 命令替换的反引号已经被搁置了一段时间,因为它很容易出错。请改用currentTime=$(date +%H%M%S)

答案 1 :(得分:1)

听起来这就是你想要的:

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