我曾经crontab -e
定义以下内容:
*/5 * * * * env DISPLAY=:0 /home/max/.bin/battery-check.sh
脚本的内容是:
#!/bin/bash
# Battery level warning script
battery_level=`acpi -b | grep -P -o '[0-9]+(?=%)'`
if [[ "$battery_level" -le "80" && "$battery_level" -gt "10" ]]
then
notify-send "Battery low warning" "
Battery level is ${battery_level}%"
elif [[ "$battery_level" -le "10" && "$battery_level" -gt "5" ]]
then
notify-send "Battery very low warning" "
Battery level is ${battery_level}%
Use ac power now,
or shutdown is imminent - close applications"
else [[ "$battery_level" -le "5" ]]
# our custom actions for critical battery level
notify-send "Battery critical warning" "
Battery level is ${battery_level}%
Shutting system down now"
fi
有些语句(例如第一个语句-le "80"
)仅用于测试/调试目的。该脚本的问题在于,它会导致每 5 分钟弹出一个报告最后一个条件的窗口:
电池严重警告
电池电量为 100%
正在关闭系统
答案1
前两个条件都不满足,因此else
执行的块包括:
[[ "$battery_level" -le "5" ]] # evaluates this, which does nothing
notify-send ... # shows the message you are seeing
这就是你想要的:
#!/bin/bash
# Battery level warning script
battery_level=`acpi -b | grep -oP '[0-9]+(?=%)'`
if [[ $battery_level -le 80 && $battery_level -gt 10 ]]; then
notify-send "Battery low warning" "
Battery level is ${battery_level}%"
elif [[ $battery_level -le 10 && $battery_level -gt 5 ]]; then
notify-send "Battery very low warning" "
Battery level is ${battery_level}%
Use ac power now,
or shutdown is imminent - close applications"
elif [[ $battery_level -le 5 ]]; then
# our custom actions for critical battery level
notify-send "Battery critical warning" "
Battery level is ${battery_level}%
Shutting system down now"
fi