battery.sh (655B)
1 #!/bin/sh 2 3 # Check if battery directories are detected 4 [ ! -e /sys/class/power_supply/BAT?* ] && echo "No battery found" && exit 1 5 6 # Loop through all attached batteries and format the info 7 for battery in /sys/class/power_supply/BAT?* 8 do 9 # Sets up the status and capacity 10 status=$(cat "$battery/status") 11 case "$status" in 12 "Charging") icon="" ;; 13 *) icon="" ;; 14 esac 15 capacity=$(cat "$battery/capacity") 16 # Will make a warn variable if discharging and low 17 warn="" 18 [ "$status" = "Discharging" ] && [ "$capacity" -le 10 ] && warn=" LOW BATTERY " 19 # Prints the info 20 printf "%s%s %03d%%\n" "$warn" "$icon" "$capacity"; 21 done