diff options
Diffstat (limited to 'power_now')
-rwxr-xr-x | power_now | 16 |
1 files changed, 11 insertions, 5 deletions
@@ -12,10 +12,16 @@ while getopts "b:mqt" o; do case "${o}" in b) BATTERY="$OPTARG" ;; esac done +error () { + printf "%s\n" "$@" + exit 1 +} + #TOTALPOWER='0' 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 DIRNAME="/sys/class/power_supply/${battery}" if [ -f "${DIRNAME}/power_now" ]; then @@ -26,9 +32,9 @@ case "$BATTERY" in fi elif [ -f "${DIRNAME}/current_now" ]; then if [ -n "${MILLIWATTS}" ]; then - POWER="$(cat "${DIRNAME}/current_now" "${DIRNAME}/voltage_now" | awk '{print $1*$2/1e9}' | head -1)" + POWER="$(cat "${DIRNAME}/current_now" "${DIRNAME}/voltage_now" | paste -d' ' -s | awk '{print $1*$2/1e9}' | head -1)" else - POWER="$(cat "${DIRNAME}/current_now" "${DIRNAME}/voltage_now" | awk '{print $1*$2/1e12}' | head -1)" + POWER="$(cat "${DIRNAME}/current_now" "${DIRNAME}/voltage_now" | paste -d' ' -s | awk '{print $1*$2/1e12}' | head -1)" fi fi if [ -n "${TOTAL}" ]; then @@ -59,7 +65,7 @@ case "$BATTERY" in ;; *) DIRNAME="/sys/class/power_supply/${BATTERY}" - [ -d "${DIRNAME}" ] || echo "No such battery!" + [ -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)" @@ -68,9 +74,9 @@ case "$BATTERY" in fi elif [ -f "${DIRNAME}/current_now" ]; then if [ -n "${MILLIWATTS}" ]; then - POWER="$(cat "${DIRNAME}/current_now" "${DIRNAME}/voltage_now" | awk '{print $1*$2/1e9}' | head -1)" + POWER="$(cat "${DIRNAME}/current_now" "${DIRNAME}/voltage_now" | paste -d' ' -s | awk '{print $1*$2/1e9}' | head -1)" else - POWER="$(cat "${DIRNAME}/current_now" "${DIRNAME}/voltage_now" | awk '{print $1*$2/1e12}' | head -1)" + POWER="$(cat "${DIRNAME}/current_now" "${DIRNAME}/voltage_now" | paste -d' ' -s | awk '{print $1*$2/1e12}' | head -1)" fi fi if [ -z "${QUIET}" ]; then |