首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无限期的循环?如果联机或脱机,请检查两台打印机-如果关闭,请发送电子邮件

无限期的循环?如果联机或脱机,请检查两台打印机-如果关闭,请发送电子邮件
EN

Stack Overflow用户
提问于 2021-05-11 16:11:32
回答 1查看 27关注 0票数 0

我有一个正在无限期循环的脚本--有两台打印机要检查一次-- printer01和printer02。如果他们被关闭或关闭,然后发送电子邮件。为什么它在第一个printer01上无限期地循环?

代码语言:javascript
复制
#!/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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-05-11 16:55:11

考虑在每个外部命令之后添加错误检查。

您可以使用ksh -x来运行您的脚本来查看您的错误,这会打开调试模式。还可以通过在脚本中添加set -x来做到这一点。

您的脚本正在循环,因为变量$c没有按照您的想法设置。

您可能需要检查退出代码($?)从每个外部命令中,还检查lpstat返回的stdout是否为空或任何错误,并验证邮件程序是否在路径和可执行文件等上。

尝试另一种方法,如下所示:

代码语言:javascript
复制
#!/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
exit
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67490554

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档