我有一个正在无限期循环的脚本--有两台打印机要检查一次-- printer01和printer02。如果他们被关闭或关闭,然后发送电子邮件。为什么它在第一个printer01上无限期地循环?
#!/bin/ksh
c=1
while [[ $c -le 3 ]]; do
STATUS=$(lpstat -printer0$c| grep 'READY' | awk '{print $3}')
if [[ ! $STATUS == "DOWN" ||! $STATUS == "OFF" ]] ; then
#create email summary and send
today=`date +%m%d%Y`
echo "*** START EMAIL SUMMARY *** " > /lsf10/monitors/lpstat_email_$today.txt
echo "* " >> /lsf10/monitors/lpstat_email_$today.txt
echo "* Date - `date` " >> /lsf10/monitors/lpstat_email_$today.txt
echo "* " >> /lsf10/monitors/lpstat_email_$today.txt
echo "* Start time => $s_time " >> /lsf10/monitors/lpstat_email_$today.txt
echo "* Current time => `date +%H:%M:%S` " >> /lsf10/monitors/lpstat_email_$today.txt
echo "* " >> /lsf10/monitors/lpstat_email_$today.txt
echo "* Printer email message will be here for you to modify " >> /lsf10/monitors/lpstat_email_$today.txt
echo "* Please investigate. " >> /lsf10/monitors/lpstat_email_$today.txt
echo "* " >> /lsf10/monitors/lpstat_email_$today.txt
echo "*** END EMAIL SUMMARY *** " >> /lsf10/monitors/lpstat_email_$today.txt
`mail -s "Mobius Printer Down Alert!" "email1,email2" < /lsf10/monitors/lpstat_email_$today.txt`
fi
c = $c + 1
done
exit发布于 2021-05-11 16:55:11
考虑在每个外部命令之后添加错误检查。
您可以使用ksh -x来运行您的脚本来查看您的错误,这会打开调试模式。还可以通过在脚本中添加set -x来做到这一点。
您的脚本正在循环,因为变量$c没有按照您的想法设置。
您可能需要检查退出代码($?)从每个外部命令中,还检查lpstat返回的stdout是否为空或任何错误,并验证邮件程序是否在路径和可执行文件等上。
尝试另一种方法,如下所示:
#!/bin/ksh
typeset +i c=1
while (( c <= 2 )); do
STATUS=$(lpstat -printer0$c| grep 'READY' | awk '{print $3}')
if [[ ! $STATUS == "DOWN" ||! $STATUS == "OFF" ]] ; then
#create email summary and send
today=`date +%m%d%Y`
outfile=/tmp/lpstat_email_$today.txt # use your own path
echo "*** START EMAIL SUMMARY *** " > ${outfile}
echo "* " >> ${outfile}
echo "* Date - `date` " >> ${outfile}
echo "* " >> ${outfile}
echo "* Start time => $s_time " >> ${outfile}
echo "* Current time => `date +%H:%M:%S` " >> ${outfile}
echo "* " >> ${outfile}
echo "* Printer email message will be here for you to modify " >> ${outfile}
echo "* Please investigate. " >> ${outfile}
echo "* " >> ${outfile}
echo "*** END EMAIL SUMMARY *** " >> ${outfile}
`mail -s "Mobius Printer Down Alert!" "email1,email2" < ${outfile}`
fi
let c=$(( c + 1 ))
done
exithttps://stackoverflow.com/questions/67490554
复制相似问题