Linux for循环中if语句中的Bash exit
我试图在Bash中执行for循环并在if语句上退出,但我意识到它将在完成循环之前中断代码Linux for循环中if语句中的Bash exit,linux,bash,loops,Linux,Bash,Loops,我试图在Bash中执行for循环并在if语句上退出,但我意识到它将在完成循环之前中断代码 #!/bin/bash for node in $(ps -ef | awk <something>); do var=<command> echo "node ${node}" if [[ ${var} -gt 300000 ]]; then echo "WARNING!"; exit 1; elif [[ ${var} -gt 100000
#!/bin/bash
for node in $(ps -ef | awk <something>);
do
var=<command>
echo "node ${node}"
if [[ ${var} -gt 300000 ]]; then
echo "WARNING!";
exit 1;
elif [[ ${var} -gt 1000000 ]]; then
echo "CRITICAL!";
exit 2;
else
echo "OK!";
exit 0;
fi
done
如何在每个节点上正确退出 使用man bash中的
continue
操作符
...
continue [n]
Resume the next iteration of the enclosing for, while, until, or select loop. If n is specified, resume at the nth enclosing loop. n must be
≥ 1. If n is greater than the number of enclosing loops, the last enclosing loop (the ``top-level'' loop) is resumed. The return value is 0
unless n is not greater than or equal to 1.
或者,如果需要退出循环,请断开
...
break [n]
Exit from within a for, while, until, or select loop. If n is specified, break n levels. n must be ≥ 1. If n is greater than the number of
enclosing loops, all enclosing loops are exited. The return value is 0 unless n is not greater than or equal to 1.
这里有一个替代方案。它统计结果并根据计数器创建退出状态。我更改了语义,因为您的脚本从未到达关键路径。而是为大于1000000的值输入警告路径:
#!/bin/bash
let ok_count=0 warn_count=0 critical_count=0
for node in $(ps -ef | awk <command>);
do
var=<command>
echo "node ${node}"
# >1000000 must be checked before >300000
if [[ ${var} -gt 1000000 ]]; then
echo "CRITICAL!";
let critical_count++
elif [[ ${var} -gt 300000 ]]; then
echo "WARNING!";
let warn_count++
else
echo "OK!";
let ok_count++
fi
done
if ((critical_count)); then
exit 2
elif ((warn_count)); then
exit 1
else
exit 0
fi
#/bin/bash
让ok\u count=0警告\u count=0临界\u count=0
对于节点,单位为$(ps-ef | awk);
做
变量=
回显“节点${node}”
#在>300000之前,必须检查>1000000
如果[${var}-gt 1000000]];然后
回声“关键!”;
让关键的事情发生++
elif[${var}-gt 300000]];然后
回声“警告!”;
算了吧++
其他的
回声“好!”;
让我们算了++
fi
完成
如果((临界_计数));然后
出口2
elif((警告计数));然后
出口1
其他的
出口0
fi
如果只需要退出状态,可以优化此脚本:
“严重”是最高警告级别。因此,计数是不必要的。
好的,这是退路。因此,计数是不必要的
#!/bin/bash
let warn_count=0
for node in $(ps -ef | awk <command>);
do
var=<command>
echo "node ${node}"
if [[ ${var} -gt 1000000 ]]; then
echo "CRITICAL! -> abort";
exit 2 # no more analysis needed!
elif [[ ${var} -gt 300000 ]]; then
echo "WARNING!";
let warn_count++
fi
done
exit $(( warn_count > 0 ))
#/bin/bash
让警告计数=0
对于节点,单位为$(ps-ef | awk);
做
变量=
回显“节点${node}”
如果[${var}-gt 1000000]];然后
echo“严重!->中止”;
退出2#无需更多分析!
elif[${var}-gt 300000]];然后
回声“警告!”;
算了吧++
fi
完成
退出$((警告计数>0))
如果您同时获得多种状态(如一些OK、一些警告,可能是一个或两个严重警告),您希望它做什么?它应该只报告最严重的状态,还是打印所有错误并以最严重的状态退出,或者…我希望它为不同的节点发送退出代码。可以使用数组存储数据,循环完成后,使用数组重新访问状态并进行必要的计算。另外,当你说发送退出代码时,在上面的脚本中,我认为你没有发送任何退出代码。shell脚本只能退出一次,否则将无法工作。中断将中断循环,这不是我打算做的。continue也不会成功,因为它没有考虑退出代码。你是对的@Wimm,最后,如果其中一个节点有一个关键状态,它就足够退出了。
#!/bin/bash
let warn_count=0
for node in $(ps -ef | awk <command>);
do
var=<command>
echo "node ${node}"
if [[ ${var} -gt 1000000 ]]; then
echo "CRITICAL! -> abort";
exit 2 # no more analysis needed!
elif [[ ${var} -gt 300000 ]]; then
echo "WARNING!";
let warn_count++
fi
done
exit $(( warn_count > 0 ))