URI:
       tbattery - scripts - various script and utils
  HTML git clone git://z3bra.org/scripts
   DIR Log
   DIR Files
   DIR Refs
       ---
       tbattery (1220B)
       ---
            1 #!/bin/sh
            2 #
            3 # z3bra - (c) wtfpl 2014
            4 
            5 usage () {
            6     cat <<EOF
            7 usage: $(basename $0) [-hlsb]
            8     -h : print this help
            9     -l : print battery percentage (default)
           10     -s : print battery state
           11     -b : beep under critical level (see BAT_BELL)
           12 
           13 environment:
           14     CRITICAL : the critical state level
           15     BAT_BELL : the command to run when run with -b flag under CRITICAL level
           16 EOF
           17 }
           18 
           19 # if battery is under a critical level, $BAT_BELL will be run
           20 bell () {
           21     # don't do anything if we're over the critical level, or the battery is
           22     # charging
           23     test ${BATC} -gt ${CRITICAL}    && return 0
           24     test ${BATS} != "Discharging"   && return 0
           25 
           26     $BAT_BELL
           27 }
           28 
           29 # output the current battery level
           30 level () {
           31     echo "${BATC}%"
           32 }
           33 
           34 # print the current battery state
           35 state () {
           36     echo "${BATS}"
           37 }
           38 
           39 # get battery name
           40 BATN=$(ls /sys/class/power_supply/ | grep BAT)
           41 
           42 # exit if no battery available
           43 test -z "$BATN" && exit 1
           44 
           45 # get battery level and status (charging or not)
           46 BATC=`cat /sys/class/power_supply/${BATN}/capacity`
           47 BATS=`cat /sys/class/power_supply/${BATN}/status`
           48 
           49 CRITICAL=${CRITICAL:-7}
           50 BAT_BELL=${BAT_BELL:-beep -f 1000 -l 200}
           51 
           52 case $1 in
           53     -h) usage ;;
           54     -s) state ;;
           55     -b) bell ;;
           56      *) level ;;
           57  esac