Bash脚本或问题

我有下面的脚本,基本上我想要做的是连接到各种服务器。 查看有多less个打开的文件,如果超过5万个,就发送一封电子邮件。 但最后一个服务器,我需要这个检查超过40万。 我有一个问题,或没有工作。

如果我按原样运行这个当前脚本,即使没有任何限制被触发,我也会收到警报邮件。 请用我的OR语句帮助我。

我在命令行输出显示:

Alert Server Issue AlertError S1:6326 S2:6355 S3:6242 S4:7171 S5:4694 S6:5284 S7:3973 S8:308432 

剧本:

 #!/bin/bash S1_IP=72.15.97.13 S2_IP=72.15.97.14 S3_IP=72.15.97.15 S4_IP=72.15.97.16 S5_IP=72.15.97.17 S6_IP=72.15.97.18 S7_IP=72.15.97.19 S8_IP=72.15.97.20 RESULT1=$(ssh test@$S1_IP lsof | wc -l) RESULT2=$(ssh test@$S2_IP lsof | wc -l) RESULT3=$(ssh test@$S3_IP lsof | wc -l) RESULT4=$(ssh test@$S4_IP lsof | wc -l) RESULT5=$(ssh test@$S5_IP lsof | wc -l) RESULT6=$(ssh test@$S6_IP lsof | wc -l) RESULT7=$(ssh test@$S7_IP lsof | wc -l) RESULT8=$(ssh test@$S8_IP lsof | wc -l) ERROR_COUNT=0 if [[ $RESULT1 || $RESULT2 || $RESULT3 || $RESULT4 || $RESULT5 || $RESULT6 || $RESULT7 -gt 50000 ]] || [[ $RESULT8 -gt 400000 ]] then ERRORS[$ERROR_COUNT]="AlertError" ERROR_COUNT=$(($ERROR_COUNT+1)) fi if [ $ERROR_COUNT -gt 0 ] then [email protected] SUBJECT="Over 50,000" BODY='Alert Server Issue' CNT=0 while [ "$CNT" != "$ERROR_COUNT" ] do BODY="$BODY ${ERRORS[$CNT]} S1:$RESULT1 S2:$RESULT2 S3:$RESULT3 S4:$RESULT4 S5:$RESULT5 S6:$RESULT6 S7:$RESULT7 S8:$RESULT8" CNT=$(($CNT+1)) done echo $SUBJECT echo $BODY echo $BODY | mail -s "$SUBJECT" -a "From: [email protected]" $EMAIL else echo "I can handle it S1:$RESULT1 S2:$RESULT2 S3:$RESULT3 S4:$RESULT4 S5:$RESULT5 S6:$RESULT6 S7:$RESULT7 S8:$RESULT8" fi 

信息coreutils'testing调用'

 If EXPRESSION is a single argument, 'test' returns false if the argument is null and true otherwise 

所以,在你的代码中有以下代码评估为true

 if [[ $RESULT1 || ... 

你想为所有的参数显式地指定$ RESULT1 -gt 50000 ,而不是只为最后一个。

随着我的帽子closuresnkms,我会build议下面的代替:

 #!/bin/bash RESULT[1]=2 RESULT[2]=4 RESULT[3]=8 RESULT[4]=16 RESULT[5]=99999 RESULT[6]=32 RESULT[7]=64 RESULT[8]=128 RESULT[9]=256 for result in ${RESULT[*]} do if [[ $result -gt 50000 ]]; then echo big result $result fi done 

重复-gt 50000一遍又一遍是容易出错的。 即使一旦有人出现,发生了什么事情,并试图增加另一个结果? 循环更容易维护和扩展。 如果你要循环每个结果,为什么不把它们放到一个数组中呢?

如果你想扩展到脚本的其余部分,你可以把你的IP地址放入一个数组中。