Reputation: 495
I've been fumbling about with this bash script for a while now but I'm not familiar enough with bash scripts to figure out the problem. Here is my script:
#!/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
The goal is to get notify-send warnings when my battery is discharging and at certain levels of charge. The error I get is
./battery_check.sh: line 16: [: missing `]'
./battery_check.sh: line 20: [: missing `]'
./battery_check.sh: line 24: [: missing `]'
Battery > 60%: waiting 10 secs
Upvotes: 0
Views: 266
Reputation: 368
You have syntax errors on 16th, 20th and 24th lines. It should be
if [ $NOTIFIED -lt "1" ] && [ $BATTERY_LEVEL -lt "50" ] && [ $BATTERY_LEVEL -gt "10" ];
Upvotes: 0
Reputation: 1510
Change:
if [ $NOTIFIED -lt "1" && $BATTERY_LEVEL -lt "50" && $BATTERY_LEVEL -gt "10" ]; then`
to
if [ "$NOTIFIED" -lt "1" ] && [ "$BATTERY_LEVEL" -lt "50" ] && [ "$BATTERY_LEVEL" -gt "10" ]; then ....`
Upvotes: 3