Adjust bat.sh to find the first battery matching /sys/class/power_supply/BAT[0-9].
authorW. Trevor King <wking@drexel.edu>
Mon, 21 Nov 2011 03:21:22 +0000 (22:21 -0500)
committerW. Trevor King <wking@drexel.edu>
Mon, 21 Nov 2011 03:21:22 +0000 (22:21 -0500)
posts/bat/bat.sh

index b7c0a5c5609c8c3987d0b79d90a1265714dd8097..a66901d34ad463e8c4ed56e7a7429b8a986cef5b 100755 (executable)
 # 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%)"