From 62c5df3ab5e2a7f9087bd92941ac1d171a29186b Mon Sep 17 00:00:00 2001 From: Harald Pfeiffer Date: Wed, 27 Jun 2018 13:44:00 +0200 Subject: Code sanitation and line length limiting --- bin/batman | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/bin/batman b/bin/batman index 5b873d7..9928931 100755 --- a/bin/batman +++ b/bin/batman @@ -1,24 +1,31 @@ #!/bin/sh -declare -x VOERBOOS +export VOERBOOS RETVAL=0;export RETVAL -BATVAL="$(echo "scale=0;$(cat /sys/class/power_supply/BAT0/charge_now)*100/$(cat /sys/class/power_supply/BAT0/charge_full)"|bc -l)";export BATVAL -BATSTATE="`cat /sys/class/power_supply/BAT0/status`";export BATSTATE +CNOW="$(cat /sys/class/power_supply/BAT0/charge_now)" +CFUL="$(cat /sys/class/power_supply/BAT0/charge_full)" +BATVAL="$(echo "scale=0;$CNOW*100/$CFUL"|bc -l)";export BATVAL +unset CNOW CFUL +BATSTATE="$(cat /sys/class/power_supply/BAT0/status)";export BATSTATE BATFILE="/tmp/batman.sd";export BATFILE export OPTCNT=0 -function batmobile { +batmobile() { if [ "$BATVAL" -le 5 ];then case "$BATSTATE" in "Charging") MSG="Battery charged at $BATVAL% with status \"$BATSTATE\", no shutdown." - [ ! -z "$VOERBOOS" ]&&[ "$VOERBOOS" -eq 1 ]&&echo "$MSG"||true - logger -p"info" -t"batman" "Battery charged at $BATVAL% with status \"$BATSTATE\", no shutdown."||RETVAL=$(("$RETVAL"+$?)) + [ ! -z "$VOERBOOS" ]&&[ "$VOERBOOS" -eq 1 ]&&echo "$MSG" + LMSG="Battery charged at $BATVAL% with status \"$BATSTATE\", no shutdown." + logger -p"info" -t"batman" "$LMSG"||RETVAL=$(("$RETVAL"+$?)) + unset LMSG [ -e "$BATFILE" ]&&/usr/sbin/shutdown -c >/dev/null 2>&1&&rm "$BATFILE" SDCRV="$?" case "$SDCRV" in 0) logger -p"info" -t"batman" "Shutdown cancelled.";; *) - logger -p"warn" -t"batman" "$BATFILE exists but couldn't cancel shutdown!" + LMSG="batman" "$BATFILE exists but couldn't cancel shutdown!" + logger -p"warn" -t"$LMSG" + unset LMSG RETVAL=$(("$RETVAL"+"$SDCRV")) ;; esac @@ -26,30 +33,36 @@ function batmobile { *) MSG="Battery charged at $BATVAL%, powering down." [ ! -z "$VOERBOOS" ]&&[ "$VOERBOOS" -eq 1 ]&&echo "$MSG"||true - logger -pwarn -t"batman" "$MSG"||RETVAL=$(("$RETVAL"+$?)) + logger -pwarn -t"batman" "$MSG";RETVAL=$((RETVAL+"$?")) if [ ! -e "$BATFILE" ];then - /usr/sbin/shutdown -P +2 "Battery low and discharging, powering down. (batman)"&&touch "$BATFILE"||RETVAL=$(("$RETVAL"+$?)) + SMSG="Battery low and discharging, powering down. (batman)" + /usr/sbin/shutdown -P +2 "$SMSG"&&touch "$BATFILE" + RETVAL=$((RETVAL+"$?")) fi ;; esac else MSG="Battery charged at $BATVAL%, no shutdown." - [ ! -z "$VOERBOOS" ]&&[ "$VOERBOOS" -eq 1 ]&&echo "$MSG"||logger -p"info" -t"batman" "$MSG"||RETVAL=$(("$RETVAL"+$?)) + [ ! -z "$VOERBOOS" ]&&[ "$VOERBOOS" -eq 1 ]&&echo "$MSG"||logger -p"info" -t"batman" "$MSG" + RETVAL=$((RETVAL+"$?")) fi } -function batarang { +batarang() { MSG="$BATVAL% ($BATSTATE)";echo "$MSG"; } while getopts :vnp SHOPT;do case "$SHOPT" in - v)VOERBOOS=1;export VOERBOOS;((OPTCNT++));; - p)batmobile;((OPTCNT++));; - ""|n)batarang;((OPTCNT++));; - *)echo "Not supported option: -""${OPTARG}" >&2;RETVAL=$(($RETVAL+1)) >&2;WRONGOPT=1;((OPTCNT++));; + v)VOERBOOS=1;export VOERBOOS;OPTCNT=$((OPTCNT+1));; + p)batmobile;OPTCNT=$((OPTCNT+1));; + n)batarang;OPTCNT=$((OPTCNT+1));; + *) + echo "Not supported option: -""${OPTARG}" >&2 + RETVAL=$((RETVAL+1));WRONGOPT=1 ;OPTCNT=$((OPTCNT+1)) + ;; esac done -shift $(( $OPTIND - 1 )) +shift $((OPTIND-1)) [ "$OPTCNT" -le 0 ]&&batarang -[ ! -z "$WRONGOPT" ]&&[ "$WRONGOPT" -eq 1 ]&&exit 1||true +[ ! -z "$WRONGOPT" ]&&[ "$WRONGOPT" -eq 1 ]&&exit 1;true exit "$RETVAL" -- cgit v1.2.3