我已经对这个bash脚本感到困惑了一段时间了,但是我对bash脚本并不十分了解以解决问题。这是我的脚本:
#!/usr/bin/bash
# Battery level warning script
NOTIFIED=0
while true; do
BATTERY_LEVEL=$(cat /sys/class/power_supply/BAT0/capacity)
STATE=$(cat /sys/class/power_supply/BAT0/status)
echo "beep"
# notify user of battery level
if [ $STATE == "Discharging" ] ; then
echo "Discharging"
if [ $NOTIFIED -lt "1" && $BATTERY_LEVEL -lt "50" && $BATTERY_LEVEL -gt "10" ]; then
echo "Battery >10%, < 50%"
sudo -u korgan DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send "Battery low warning" "Battery level is ${BATTERY_LEVEL}%" --icon=battery-low
NOTIFIED=1
elif [ $NOTIFIED -lt "2" && $BATTERY_LEVEL -lt "11" && $BATTERY_LEVEL -gt "5" ]; then
echo "Battery >5%, <11%"
sudo -u korgan DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send "Battery very low warning" "Battery level is ${BATTERY_LEVEL}% Use ac power now, or shutdown is imminent - close applications" --icon=battery-caution
NOTIFIED=2
elif [ $NOTIFIED -lt "3" && $BATTERY_LEVEL -lt "6" ]; then
echo "Battery <6%"
sudo -u korgan DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send "Battery critical warning" "Battery level is ${BATTERY_LEVEL}% Shutting system down now"
NOTIFIED=3
fi
elif [ $STATE == "Charging" ]; then
echo "Charging"
NOTIFIED=0
fi
if [ $BATTERY_LEVEL -gt "60" ]; then
echo "Battery > 60%: waiting 10 secs"
sleep 10
else
echo "Battery <=60%: waiting 5 secs"
sleep 5
fi
done
目标是在电池放电且处于一定电量时获得通知发送警告。我得到的错误是
./battery_check.sh: line 16: [: missing `]'
./battery_check.sh: line 20: [: missing `]'
./battery_check.sh: line 24: [: missing `]'
Battery > 60%: waiting 10 secs
答案 0 :(得分:3)
更改:
if [ $NOTIFIED -lt "1" && $BATTERY_LEVEL -lt "50" && $BATTERY_LEVEL -gt "10" ]; then`
到
if [ "$NOTIFIED" -lt "1" ] && [ "$BATTERY_LEVEL" -lt "50" ] && [ "$BATTERY_LEVEL" -gt "10" ]; then ....`
答案 1 :(得分:0)
您在第16、20和24行有语法错误。应该是
if [ $NOTIFIED -lt "1" ] && [ $BATTERY_LEVEL -lt "50" ] && [ $BATTERY_LEVEL -gt "10" ];