Skip to content

Instantly share code, notes, and snippets.

@valvallow
Created November 19, 2011 18:58
Show Gist options
  • Save valvallow/1379218 to your computer and use it in GitHub Desktop.
Save valvallow/1379218 to your computer and use it in GitHub Desktop.
battery.sh
#!/bin/sh
current=`grep "remaining" /proc/acpi/battery/*/state | sed 's/[a-zA-Z: ]//g'`
total=`grep "design capacity:" /proc/acpi/battery/*/info | sed 's/[a-zA-Z: ]//g'`
rate=`grep "present rate:" /proc/acpi/battery/*/state | sed 's/[a-zA-Z: ]//g'`
charging=`grep "charging state:" /proc/acpi/battery/*/state | sed 's/[a-zA-Z ]*:[ ]*//g'`
if [ $rate -eq 0 ]; then
echo $charging
exit 0
fi
percent=`echo "100.0 * $current / (1.0*$total)" | bc`
hours=`echo "(1.0*$current) / $rate" | bc`
minutes=`echo "60.0 * $current / $rate - $hours * 60" | bc`
minutes_0=``
if [ $minutes -lt 10 ]; then
minutes_0=0
fi
echo ''$charging' '$percent'% '$hours':'$minutes_0$minutes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment