Shell – Script for “battery level reminder”

batterylaptopshell-script

I want to plug in the laptop battery charger when the battery level goes down to 40% and then plug it out when the battery charge level reaches 80%. And for that reason, I need a script that would remind me to plug in the charger when the battery level is 40% and again would remind me when the battery charge level reaches 80%. How would be the script? Could anything else do that?

Best Answer

Try this one. In my ubuntu 12.04 it is working perfectly.

#!/bin/bash
high=$(cat /sys/class/power_supply/BAT0/charge_full_design)
now=$(cat /sys/class/power_supply/BAT0/charge_now)
stat=$(cat /sys/class/power_supply/BAT0/status)
echo -e "scale=1\n$now/$high * 100\nquit"> hi
per=$(bc hi)
per=$(expr "$per" : '\(.*\)\..*')

if [ $stat == Charging ] ; then
    if [ $per -gt 80 ] ; then
            zenity --warning --text="BATTERY IS FULL REMOVE THE CHARGER"
    fi
elif [ $stat == Discharging ] ; then
    if [ $per -lt 40 ] ; then
            zenity --warning --text="BATTERY IS LOW PLUGIN THE CHARGER"

    fi
fi
rm hi