Skip to content

Commit 1891b17

Browse files
committed
Check Battery Existence before Calling and Printing Content
This ensures that the script does not return errors when operating in environments without batteries, as if there are no battery-related files in the target directory, the script now stops from accessing the files to begin with.
1 parent 5d2a760 commit 1891b17

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

scripts/battery_percentage.sh

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ source "$CURRENT_DIR/helpers.sh"
77
print_battery_percentage() {
88
# percentage displayed in the 2nd field of the 2nd row
99
if is_wsl; then
10-
local battery=$(find /sys/class/power_supply/*/capacity | tail -n1)
11-
if [ -n "$battery" ]; then
12-
echo $(cat "$battery")%
13-
return
14-
fi
10+
if [ -e /sys/class/power_supply/*/capacity ]; then
11+
echo $(cat $(find /sys/class/power_supply/*/capacity | tail -n1))%
12+
fi
1513
elif command_exists "pmset"; then
1614
pmset -g batt | grep -o "[0-9]\{1,3\}%"
1715
elif command_exists "acpi"; then

0 commit comments

Comments
 (0)