我正在尝试创建一个与 crontab 一起运行的脚本,如果有任何更新可用,脚本将发送通知。脚本本身没有给我任何错误,但是在 if 语句中只有“else”部分有效。有任何想法吗?
#!/bin/bash
sudo apt update > ~/Desktop/UpdateInfo
update1=$(grep "package can be upgraded" ~/Desktop/UpdateInfo | cut -d' ' -f2,3,4,5)
var1="package can be upgraded."
if [ var1 = update1 ]
then
notify-send "Updates are available"
else
notify-send "test"
fi
Run Code Online (Sandbox Code Playgroud)
您正在将字符串var1与字符串进行比较update1。那些永远不会相同。您想要比较变量:
if [ "$var1" = "$update1" ]
Run Code Online (Sandbox Code Playgroud)
当然,整个事情可以简化为:
sudo apt update | grep -q "package can be upgraded" && notify-send "Updates are available"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
813 次 |
| 最近记录: |