简体   繁体   English

Linux打印状态检查脚本

[英]Linux Print Status Check Script

I'm trying to write a script that checks the status of a printer (it will be set as a chron job). 我正在尝试编写一个检查打印机状态的脚本(它将被设置为同步作业)。 If the printer is running, It'll echo out that the printer is enabled. 如果打印机正在运行,则会回显打印机已启用。 If the printer is not, it should send an e mail with lpstat info. 如果不是,则应发送带有lpstat信息的电子邮件。 Either way, it should be writing the lpstat info to a txt file which will only be e mailed in the case that the printer is down. 无论哪种方式,它都应该将lpstat信息写入一个txt文件,该文件仅在打印机关闭的情况下才通过电子邮件发送。 Here is the script: 这是脚本:

#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

Here are the errors I'm getting: 这是我遇到的错误:

/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

I'm trying to run the script from the /home directory. 我正在尝试从/ home目录运行脚本。 Thanks in advance for the suggestions :) 在此先感谢您的建议:)

This line 这条线

RC=cat /tmp/printerstatus.txt | grep "enabled"

attempts to execute /tmp/printerstatus.txt (why is its execution permission set?) as a shell script. 尝试将/tmp/printerstatus.txt(为什么设置其执行权限?)作为shell脚本执行。 You want to use command substitution to capture the output of the entire pipeline (which, by the way, is a useless use of cat ) in the variable RC . 您想使用命令替换来捕获变量RC整个管道的输出(顺便说一句, cat的无用用法)。

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

You can reduce the script further to 您可以将脚本进一步简化为

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
RC=cat /tmp/printerstatus.txt | grep "enabled"

This is causing the errors. 这导致了错误。

The second argument to grep is a filename. grep的第二个参数是文件名。 cat`ting to grep is redundant. 抄袭grep是多余的。 You should 你应该

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM