st file
failed" "aa" "2018-04-03T17:43:38Z" "2018-04-03T18:43:38Z"
我有代码将行读入变量并将其与不同的值(“成功”,“失败”,“中止”)进行比较,并写入适当的输出。
while read -r status name startdate enddate; do
case "$status" in
"\"aborted\"")
echo "Job " $name "aborted"
;;
"\"failed\"")
echo "Job " $name "failed" " In project Demo"
;;
"\"succeeded\"")
echo "Job " $name "success"
;;
#*) echo 0 + $name
esac
exit 0
done<st.txt
现在想在现有的case语句中添加另一个条件:比较两个日期,如果它们之间的差异大于5分钟并且状态为“aborted”print “aborted-long running”,如果时差小于5分钟并且状态为“中止”,则只需打印“中止”,其他条件无需更改任何内容
我导入了另一个变量elapsed
,它将计算开始日期和结束日期之间的差异
是否可以将此变量(与$status
一起放在case语句中)
我在代码中引入了已用变量,但不知道如何与现有案例集成
while read -r status name startdate enddate; do
startdate=${startdate//\"/}
stime=$(date -d "${startdate/T/ }" +%s)
enddate=${enddate//\"/}
etime=$(date -d "${enddate/T/ }" +%s)
let elapsed=etime-stime
case "$status" in
"\"aborted\"")
echo "Job " $name "aborted"
;;
"\"failed\"")
echo "Job " $name "failed"
;;
"\"succeeded\"")
echo "Job " $name "success"
;;
*) echo 0 + $name
esac
exit 0
done<st.txt
答案 0 :(得分:4)
只需向已中止的案例分支添加更多代码:
case "$status" in
'"aborted"')
[[ $elapsed -gt 300 ]] && extra="-long running" || extra=""
echo "Job $name aborted $extra"
;;
'"failed"')
echo "Job $name failed"
;;
'"succeeded"')
echo "Job $name success"
;;
*) echo "0 + $name" ;;
esac
我建议您在代码上使用缩进。
答案 1 :(得分:0)
多个IF语句:我认为它更复杂
if [ "$status" == "\"aborted\"" ] && [ "$elapsed" -gt 300 ]; then echo "Long Running Process-Demo"
elif [ "$status" == "\"aborted\"" ] && [ "$elapsed" -lt 300 ]; then echo "Aborted-Demo"
elif [ "$status" == "\"failed\"" ]; then echo "Job" $name "failed-Demo"
elif [ "$status" == "\"succeeded\"" ]; then echo "Job" $name "Succeded"
fi