diff options
Diffstat (limited to 'power_now')
-rwxr-xr-x | power_now | 33 |
1 files changed, 22 insertions, 11 deletions
@@ -2,6 +2,15 @@ unset QUIET MILLIWATTS TOTAL BATTERY="ALL" +printhelp () { + printf "power_now: get the current power draw from the battery.\n" + printf "\t-q) print only the number\n" + printf "\t-m) use milliwatts instead of watts\n" + printf "\t-t) use the total of all batteries\n" + printf "\t-b X) use battery X\n" + exit 1 +} + while getopts "b:mqt" o; do case "${o}" in q) QUIET="y" ;; m) MILLIWATTS="y" ;; @@ -10,6 +19,7 @@ while getopts "b:mqt" o; do case "${o}" in BATTERY="ALL" ;; b) BATTERY="$OPTARG" ;; + *) printhelp ;; esac done error () { @@ -21,14 +31,15 @@ error () { case "$BATTERY" in "ALL") - ls -1 /sys/class/power_supply | grep -q 'BAT' || error "Are there no batteries?" - for battery in $(ls -1 /sys/class/power_supply | grep 'BAT'); do + find /sys/class/power_supply -name 'BAT*' || error "Are there no batteries?" + for battery in /sys/class/power_supply/BAT*; do + battery="$(basename "$battery")" DIRNAME="/sys/class/power_supply/${battery}" if [ -f "${DIRNAME}/power_now" ]; then if [ -n "${MILLIWATTS}" ]; then - POWER="$(cat "${DIRNAME}/power_now" | awk '{print $1/1e3}' | head -1)" + POWER="$(awk '{print $1/1e3}' "${DIRNAME}/power_now" | head -1)" else - POWER="$(cat "${DIRNAME}/power_now" | awk '{print $1/1e6}' | head -1)" + POWER="$(awk '{print $1/1e6}' "${DIRNAME}/power_now" | head -1)" fi elif [ -f "${DIRNAME}/current_now" ]; then if [ -n "${MILLIWATTS}" ]; then @@ -41,9 +52,9 @@ case "$BATTERY" in TOTALPOWER="$(echo "${TOTALPOWER}" "${POWER}" | awk '{print $1+$2}'| head -1)" else if [ -z "${QUIET}" ]; then - printf "Power in ${battery} is " + printf "Power in %s is " "${battery}" fi - printf "${POWER}" + printf "%s" "${POWER}" if [ -n "${MILLIWATTS}" ]; then echo " mW" else @@ -55,7 +66,7 @@ case "$BATTERY" in if [ -z "${QUIET}" ]; then printf "Total power is " fi - printf "${TOTALPOWER}" + printf "%s" "${TOTALPOWER}" if [ -n "${MILLIWATTS}" ]; then echo " mW" else @@ -68,9 +79,9 @@ case "$BATTERY" in [ -d "${DIRNAME}" ] || error "No such battery!" if [ -f "${DIRNAME}/power_now" ]; then if [ -n "${MILLIWATTS}" ]; then - POWER="$(cat "${DIRNAME}/power_now" | awk '{print $1/1e3}' | head -1)" + POWER="$(awk '{print $1/1e3}' "${DIRNAME}/power_now" | head -1)" else - POWER="$(cat "${DIRNAME}/power_now" | awk '{print $1/1e6}' | head -1)" + POWER="$(awk '{print $1/1e6}' "${DIRNAME}/power_now" | head -1)" fi elif [ -f "${DIRNAME}/current_now" ]; then if [ -n "${MILLIWATTS}" ]; then @@ -80,9 +91,9 @@ case "$BATTERY" in fi fi if [ -z "${QUIET}" ]; then - printf "Power in ${BATTERY} is " + printf "Power in %s is " "${BATTERY}" fi - printf "${POWER}" + printf "%s" "${POWER}" if [ -n "${MILLIWATTS}" ]; then echo " mW" else |