diff --git a/bin/battery b/bin/battery index f1db841..93ed99c 100755 --- a/bin/battery +++ b/bin/battery @@ -1,18 +1,11 @@ #!/bin/sh -# adapted from other script i wrote, also in the public domain -if [ "$(uname)" = "Linux" ]; then - # this is a dirty hack. - # acpi | awk '{print $4}' will print the battery percentage, - # and s/,$// strips the trailing comma. - printf "%s\n" "$(acpi | awk '{print $4}' | sed 's/,$//')" -elif [ "$(uname)" = "NetBSD" ]; then - printf "%s\n" "$(envstat -s acpibat0:charge \ - | sed 1,2d \ - | cut -d ':' -f 2 \ - | awk '{print $1}' \ - | cut -d '.' -f 1)" -else +# battery -> battery.linux battery.netbsd etc +subprogram="$0"."$(uname | lowercase)" + +if ! command -v "$subprogram" >/dev/null 2>&1 && ! test -e "$subprogram"; then printf "%s: unsupported system\n" "$(uname)" 1>&2 - exit 1 + exit 70 # sysexits(3) EX_SOFTWARE fi + +"$subprogram" | exit 70 # sysexits(3) EX_SOFTWARE diff --git a/bin/battery.linux b/bin/battery.linux new file mode 100755 index 0000000..adc49d7 --- /dev/null +++ b/bin/battery.linux @@ -0,0 +1,6 @@ +#!/bin/sh + +# this is a dirty hack. +# acpi | awk '{print $4}' will print the battery percentage, +# and s/,$// strips the trailing comma. +acpi | awk '{print $4}' | sed 's/,$//' diff --git a/bin/battery.netbsd b/bin/battery.netbsd new file mode 100755 index 0000000..7e32eff --- /dev/null +++ b/bin/battery.netbsd @@ -0,0 +1,7 @@ +#!/bin/sh + +envstat -s acpibat0:charge \ + | sed 1,2d \ + | cut -d ':' -f 2 \ + | awk '{print $1}' \ + | cut -d '.' -f 1