Linux打印状态检查脚本

Linux打印状态检查脚本,linux,bash,shell,printing,conditional,Linux,Bash,Shell,Printing,Conditional,我正在尝试编写一个脚本来检查打印机的状态(它将被设置为计时作业)。如果打印机正在运行,它将回显打印机已启用。如果打印机未启动,则应发送一封包含lpstat信息的电子邮件。无论哪种方式,它都应该将lpstat信息写入一个txt文件,该文件只有在打印机关闭时才会通过电子邮件发送。以下是脚本: #This script is designed to check the status of a printer. #The printer's status attributes will be writt

我正在尝试编写一个脚本来检查打印机的状态(它将被设置为计时作业)。如果打印机正在运行,它将回显打印机已启用。如果打印机未启动,则应发送一封包含lpstat信息的电子邮件。无论哪种方式,它都应该将lpstat信息写入一个txt文件,该文件只有在打印机关闭时才会通过电子邮件发送。以下是脚本:

#This script is designed to check the status of a printer.
#The printer's status attributes will be written to a text file.
#Finally, ane mail will be sent from the command line containing the text file.
lpstat -t -h <host name> -p <printer name> > /tmp/printerstatus.txt
RC=cat /tmp/printerstatus.txt | grep "enabled"
if [ -z $RC ]
then
mail -s "Printer Status" ****.********@******.com < /tmp/printerstatus.txt
else
echo "Printer Enabled";
fi
#此脚本用于检查打印机的状态。
#打印机的状态属性将写入文本文件。
#最后,将从包含文本文件的命令行发送电子邮件。
lpstat-t-h-p>/tmp/printerstatus.txt
RC=cat/tmp/printerstatus.txt | grep“已启用”
如果[-z$RC]
然后
邮件-s“打印机状态”*********@********.com
以下是我得到的错误:

/tmp/printerstatus.txt: line 1: scheduler: command not found
/tmp/printerstatus.txt: line 2: system: command not found
/tmp/printerstatus.txt: line 3: device: command not found
/tmp/printerstatus.txt: line 4: <printer name>: command not found
/tmp/printerstatus.txt: line 5: printer: command not found
/tmp/printerstatus.txt: line 6: Paused: command not found
/tmp/printerstatus.txt: line 7: printer: command not found
/tmp/printerstatus.txt: line 8: Paused: command not found
./checkprinter.sh: line 12: syntax error: unexpected end of file
/tmp/printerstatus.txt:第1行:调度程序:未找到命令
/tmp/printerstatus.txt:第2行:未找到系统:命令
/tmp/printerstatus.txt:第3行:未找到设备:命令
/tmp/printerstatus.txt:未找到第4行::命令
/tmp/printerstatus.txt:第5行:未找到打印机:命令
/tmp/printerstatus.txt:第6行:暂停:未找到命令
/tmp/printerstatus.txt:第7行:未找到打印机:命令
/tmp/printerstatus.txt:第8行:暂停:未找到命令
./checkprinter.sh:第12行:语法错误:文件意外结束
我正在尝试从/home目录运行脚本。提前感谢您的建议:)

这一行

RC=cat /tmp/printerstatus.txt | grep "enabled"
尝试将/tmp/printerstatus.txt(为什么设置了其执行权限?)作为shell脚本执行。您希望使用命令替换来捕获变量
RC
中整个管道的输出(顺便说一下,这是对
cat
的无用使用)

RC=$(grep "enabled" /tmp/printerstatus.txt)
您可以将脚本进一步简化为

if lpstat -t -h <host name> -p <printer name> | tee /tmp/printerstatus.txt | grep -q "enabled"; then
    mail -s "Printer Status" ****.********@******.com < /tmp/printerstatus.txt
else
    echo "Printer Enabled";
fi
如果lpstat-t-h-p | tee/tmp/printerstatus.txt | grep-q“已启用”;然后
邮件-s“打印机状态”*********@********.com
这是导致错误的原因

grep的第二个参数是文件名。猫叫格雷普是多余的。你应该

RC=$(grep "enabled" /tmp/printerstatus.txt)

我读到了,这就是你如何将附件插入邮件命令的方式?获取lpinfo-l-vAwesome:)的信息现在我仍然得到的唯一错误是:语法错误:文件意外结束,这是由问题中没有显示的内容引起的。比如什么o我已经发布了整个脚本。我甚至尝试在'fi'后面添加一个EOF,使用引号将值存储在RC变量中。i、 e.RC=“.your command..”或$(),并且当您使用-z$RC时,您可以在赋值的情况下使用-z“${RC}”引号;命令替换的扩展不受分词或路径名生成的影响。错误的解释,但最终是正确的解决方案。
RC=$(grep "enabled" /tmp/printerstatus.txt)