gpt4 book ai didi

Linux 打印状态检查脚本

转载 作者:太空宇宙 更新时间:2023-11-04 04:38:58 26 4
gpt4 key购买 nike

我正在尝试编写一个脚本来检查打印机的状态(它将被设置为计时作业)。如果打印机正在运行,它将回显打印机已启用。如果打印机不是,它应该发送一封包含 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

以下是我遇到的错误:

/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

我正在尝试从/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

关于Linux 打印状态检查脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26554388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com