0

I used crontab -e to define the following:

*/5 * * * * env DISPLAY=:0 /home/max/.bin/battery-check.sh

The contents of the script are:

#!/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

Some statements, such as the first -le "80" are just for test/debug purposes. The problem with this script is that it results in a pop-up every 5 minutes reporting the last condition:

Battery critical warning
Battery level is 100%
Shutting system down now

  • What does "is the absolute_not_respect of conditionals statements" mean? – jasonwryan Oct 18 '14 at 7:00
  • What does "is the absolute_not_respect of conditionals statements" mean?... a pop-up every 5 minutes reporting ONLY the last condition: "Battery critical warning...Shutting system down now". whatever the battery percentage is – Symb932 Oct 18 '14 at 8:17
  • 1
    is it right syntax ? else [[ "$battery_level" -le "5" ]] , or elif [[ "$battery_level" -le "5" ]] – Rahul Patil Oct 18 '14 at 13:21
0

Neither of the first two conditions are met, so the else block is executed which consists of:

[[ "$battery_level" -le "5" ]]    # evaluates this, which does nothing
notify-send ...                   # shows the message you are seeing

Here's what you want:

#!/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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.