From d2d27bf2d0aacb60c57529c2b4a6f8d77bbda8b7 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 20 Nov 2011 22:21:22 -0500 Subject: [PATCH] Adjust bat.sh to find the first battery matching /sys/class/power_supply/BAT[0-9]. --- posts/bat/bat.sh | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) 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%)" -- 2.26.2