i3batwarn.sh 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/bin/bash -x
  2. #############################################
  3. # This is a simple battery warning script. #
  4. # It uses i3's nagbar to display warnings. #
  5. # #
  6. # @author agribu #
  7. #############################################
  8. # set Battery
  9. BATTERY=$(ls /sys/class/power_supply/ | grep '^BAT')
  10. # set full path
  11. ACPI_PATH="/sys/class/power_supply/$BATTERY"
  12. # get battery status
  13. STAT=$(cat $ACPI_PATH/status)
  14. # get remaining energy value
  15. REM=`grep "POWER_SUPPLY_CHARGE_NOW" $ACPI_PATH/uevent | cut -d= -f2`
  16. # get full energy value
  17. FULL=`grep "POWER_SUPPLY_CHARGE_FULL_DESIGN" $ACPI_PATH/uevent | cut -d= -f2`
  18. # get current energy value in percent
  19. PERCENT=`echo $(( $REM * 100 / $FULL ))`
  20. # set error message
  21. MESSAGE="AWW SNAP! I am running out of juice ... Please, charge me or I'll have to power down."
  22. # set energy limit in percent, where warning should be displayed
  23. LIMIT="10"
  24. # show warning if energy limit in percent is less then user set limit and
  25. # if battery is discharging
  26. if [ $PERCENT -le "$(echo $LIMIT)" ] && [ "$STAT" == "Discharging" ]; then
  27. DISPLAY=:0.0 /usr/bin/i3-nagbar -m "$(echo $MESSAGE)"
  28. fi