From: W. Trevor King Date: Mon, 21 Nov 2011 03:21:22 +0000 (-0500) Subject: Adjust bat.sh to find the first battery matching /sys/class/power_supply/BAT[0-9]. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=d2d27bf2d0aacb60c57529c2b4a6f8d77bbda8b7;p=blog.git Adjust bat.sh to find the first battery matching /sys/class/power_supply/BAT[0-9]. --- diff --git a/posts/bat/bat.sh b/posts/bat/bat.sh index b7c0a5c..a66901d 100755 --- a/posts/bat/bat.sh +++ b/posts/bat/bat.sh @@ -18,12 +18,22 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -BAT="BAT0" -SYSPATH="/sys/class/power_supply/$BAT" +for i in $(seq 0 9); do + BAT="BAT${i}" + SYSPATH="/sys/class/power_supply/$BAT" + if [ -d "${SYSPATH}" ]; then + break + fi +done + +if [ ! -d "${SYSPATH}" ]; then + echo "No battery found in $(dirname "${SYSPATH}")" + exit 1 +fi STATUS=$(cat "$SYSPATH/status") CAPACITY=$(cat "$SYSPATH/charge_full") LEVEL=$(cat "$SYSPATH/charge_now") PERCENT=$(python -c "print($LEVEL * 100. / $CAPACITY)") -echo -e "$STATUS\t$LEVEL/$CAPACITY ($PERCENT%)" +echo -e "$BAT\t$STATUS\t$LEVEL/$CAPACITY ($PERCENT%)"