Skip to content

Commit 25ef966

Browse files
authored
Fix: get status and capacity with multiple power_supplies
It is possible that $BAT contains multiple power_supplies like ```bash /sys/class/power_supply/AC /sys/class/power_supply/BAT0 /sys/class/power_supply/ucsi-source-psy-USBC000:001 ``` In such a case, the original code becomes `cat AC BAT0 ucsi-source-psy-USBC000:001/status` (path prefix omitted). It fails to run because you cannot `cat` a directory.
1 parent 66e0910 commit 25ef966

File tree

1 file changed

+12
-12
lines changed

1 file changed

+12
-12
lines changed

scripts/battery.sh

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ linux_acpi() {
99
arg=$1
1010
BAT=$(ls -d /sys/class/power_supply/*)
1111
if [ ! -x "$(which acpi 2> /dev/null)" ];then
12-
case "$arg" in
13-
status)
14-
cat $BAT/status
15-
;;
16-
17-
percent)
18-
cat $BAT/capacity
19-
;;
20-
21-
*)
22-
;;
23-
esac
12+
for DEV in $BAT; do
13+
case "$arg" in
14+
status)
15+
[ -f "$DEV/status" ] && cat "$DEV/status"
16+
;;
17+
percent)
18+
[ -f "$DEV/capacity" ] && cat "$DEV/capacity"
19+
;;
20+
*)
21+
;;
22+
esac
23+
done
2424
else
2525
case "$arg" in
2626
status)

0 commit comments

Comments
 (0)