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?
1 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
-
See Why $(..) is preferred over backticks, the difference between [ and [[ and sparing the cat with
high=$(</sys/class...jasonwryan– jasonwryan2014-12-23 04:41:52 +00:00Commented Dec 23, 2014 at 4:41 -
@jasonwryan You can edit the answer. Then Thanks for the information.Karthikeyan.R.S– Karthikeyan.R.S2014-12-23 04:51:46 +00:00Commented Dec 23, 2014 at 4:51
-
It would be a pretty major rewrite: I'll leave it to you if you are interested...jasonwryan– jasonwryan2014-12-23 05:30:05 +00:00Commented Dec 23, 2014 at 5:30
/sys/class/power_supply/BAT0/. Or if you're running upower, thenupower -i /org/freedesktop/UPower/devices/battery_BAT0zsh, then you can put battery charge level into right prompt withRPS1=$(acpi|grep -o "[0-9]*%")%. Then you can put red color on it if value is lower then 40 or higher then 80 etc.