tCreated a bunch of small utils for everyday use - scripts - various script and utils
 (HTM) git clone git://z3bra.org/scripts
 (DIR) Log
 (DIR) Files
 (DIR) Refs
       ---
 (DIR) commit 751b9f94113497dcdace3e49b4ce86d792b3c944
 (DIR) parent 06ff094435e432b33190d4f687f80a39f0744a26
 (HTM) Author: z3bra <willy@mailoo.org>
       Date:   Fri,  4 Jul 2014 15:22:45 +0200
       
       Created a bunch of small utils for everyday use
       
       Diffstat:
         M battery                             |      90 +++++++++++++++----------------
         A cpuload                             |      21 +++++++++++++++++++++
         A human                               |      67 +++++++++++++++++++++++++++++++
         A memory                              |      62 +++++++++++++++++++++++++++++++
         A network                             |      57 +++++++++++++++++++++++++++++++
         M volume                              |      27 ++++++++++++++++++++-------
         A xgroups                             |      70 +++++++++++++++++++++++++++++++
       
       7 files changed, 340 insertions(+), 54 deletions(-)
       ---
 (DIR) diff --git a/battery b/battery
       t@@ -1,61 +1,57 @@
        #!/bin/sh
        #
       -# beep once per level. does not beep when charging
       +# z3bra - (c) wtfpl 2014
        
       -# get battery name
       -BATN=$(ls /sys/class/power_supply/ | grep BAT)
       -
       -# exit if no battery available
       -test -z "$BATN" && exit 1
       -
       -# get battery level and status (charging or not)
       -BATC=`cat /sys/class/power_supply/$BATN/capacity`
       -BATS=`cat /sys/class/power_supply/$BATN/status`
       -
       -# Run this if sound is enabled
       -sbell () { 
       -    IFS=' %' read level state <<< `~/bin/volume`
       -
       -    ~/bin/volume unmute
       -    ~/bin/volume '80%'
       -
       -    beep -f 1000 -l 200 -D 50
       -
       -    case $state in
       -        on) ~/bin/volume unmute;;
       -        off) ~/bin/volume mute;;
       -    esac
       -
       -    # reset volume to its previous state
       -    ~/bin/volume "${level}%"
       +usage () {
       +    cat <<EOF
       +usage: $(basename $0) [-hlsb]
       +    -h : print this help
       +    -l : print battery percentage (default)
       +    -s : print battery state
       +    -b : beep under critical level (see BAT_BELL)
       +
       +environment:
       +    CRITICAL : the critical state level
       +    BAT_BELL : the command to run when run with -b flag under CRITICAL level
       +EOF
        }
        
       -# and this in case of no sound enabled
       -vbell () {
       -    $HOME/bin/popup "%{F#d43f10}battery $BATC%%"
       -}
       +# if battery is under a critical level, $BAT_BELL will be run
       +bell () {
       +    # don't do anything if we're over the critical level, or the battery is
       +    # charging
       +    test ${BATC} -gt ${CRITICAL}    && return 0
       +    test ${BATS} != "Discharging"   && return 0
        
       -usage () {
       -    echo "usage: `basename $0` [<low> <critical> <dead>]"
       -    exit 1
       +    $BAT_BELL
        }
        
       +# output the current battery level
        level () {
       -    echo "$BATC%"
       -    exit 0
       +    echo "${BATC}%"
        }
        
       -# If no argurments, return battery level
       -test "$#" -eq 0 && level
       -
       -# if less than 3 args, learn how to use, dumb
       -test "$#" -lt 3 && usage
       +# print the current battery state
       +state () {
       +    echo "${BATS}"
       +}
        
       -# if battery is charging, do not alert user
       -test "$BATS" = "Charging" && exit
       +# get battery name
       +BATN=$(ls /sys/class/power_supply/ | grep BAT)
        
       -test $BATC -lt $1 && sbell # one bip for level <low>
       -test $BATC -lt $2 && sbell # two bip for level <critical>
       -test $BATC -lt $3 && sbell # battery is <dead>, bip 3 times
       +# exit if no battery available
       +test -z "$BATN" && exit 1
        
       -test $BATC -lt $1 && vbell # popup a notification under <low> level
       +# get battery level and status (charging or not)
       +BATC=`cat /sys/class/power_supply/${BATN}/capacity`
       +BATS=`cat /sys/class/power_supply/${BATN}/status`
       +
       +CRITICAL=${CRITICAL:-7}
       +BAT_BELL=${BAT_BELL:-beep -f 1000 -l 200}
       +
       +case $1 in
       +    -h) usage ;;
       +    -s) state ;;
       +    -b) bell ;;
       +     *) level ;;
       + esac
 (DIR) diff --git a/cpuload b/cpuload
       t@@ -0,0 +1,21 @@
       +#!/bin/sh
       +#
       +# z3bra - (c) wtfpl 2014
       +
       +usage () {
       +    cat <<EOF
       +usage: $(basename $0) [-hp]
       +    -h : print help
       +    -p : percentage of cpu used (default)
       +EOF
       +}
       +
       +cpuperc () {
       +    LINE=`ps -eo pcpu |grep -vE '^\s*(0.0|%CPU)' |sed -n '1h;$!H;$g;s/\n/ +/gp'`
       +    echo "`bc <<< $LINE`%"
       +}
       +
       +case $1 in
       +    -h) usage;;
       +     *) cpuperc;;
       +esac
 (DIR) diff --git a/human b/human
       t@@ -0,0 +1,67 @@
       +#!/bin/sh
       +#
       +# z3bra - (c) wtfpl 2014
       +
       +usage () {
       +    cat <<EOF
       +usage: $(basename $0) [-hfbmgt] <number>
       +    -h : print help
       +    -f : detect best factorisation to use (default)
       +    -b : force output in Bytes
       +    -m : force output in Mio
       +    -g : force output in Gio
       +    -t : force output in Tio
       +
       +environment:
       +    SCALE : set the number of decimals
       +EOF
       +}
       +
       +# choose the best factore depending on the number
       +factorize () {
       +    if [ $1 -gt 1073741824 ]; then
       +        echo T
       +    elif [ $1 -gt 1048576 ]; then
       +        echo G
       +    elif [ $1 -gt 1024 ]; then
       +        echo M
       +    else
       +        echo B
       +    fi
       +}
       +
       +# perform calculation depending on expected format
       +humanize () {
       +
       +    unit=$1
       +    num=$2
       +
       +    case $unit in
       +        M) pow=1 ;;
       +        G) pow=2 ;;
       +        T) pow=3 ;;
       +        *) pow=0; unit= ;;
       +    esac
       +
       +    num=`bc <<< "scale=${SCALE}; ${num} / (1024 ^ ${pow})"`
       +
       +    echo "${num}${unit}"
       +}
       +
       +# Set the default number of decimals
       +SCALE=${SCALE:-0}
       +
       +case $1 in
       +    -h) usage;;
       +    -b) humanize B $2 ;;
       +    -m) humanize M $2 ;;
       +    -g) humanize G $2 ;;
       +    -t) humanize T $2 ;;
       +     *)
       +        # this script require at least one argument
       +        test $# -lt 1 && usage && exit 1
       +
       +
       +         humanize $(factorize $1) $1
       +         ;;
       + esac
 (DIR) diff --git a/memory b/memory
       t@@ -0,0 +1,62 @@
       +#!/bin/sh
       +#
       +# z3bra - (c) wtfpl 2014
       +
       +usage () {
       +    cat <<EOF
       +usage: $(basename $0) [-hptu]
       +    -h : print help
       +    -p : percentage of memory used (default)
       +    -t : total available memory
       +    -u : memory used (human-readable)
       +EOF
       +}
       +
       +# display the total of available memory in human readable format
       +memtotal () {
       +    read mem <<< `grep -E 'MemTotal' /proc/meminfo |awk '{print $2}'`
       +
       +    if [ $mem -gt 1048576 ]; then
       +        mem=`bc <<< "scale=2; $mem / 1048576"`
       +        mem="${mem}G"
       +    elif [ $mem -gt 1024 ]; then
       +        mem=`bc <<< "$mem / 1024"`
       +        mem="${mem}M"
       +    fi
       +
       +    echo $mem
       +}
       +
       +# display the memory used in human readable format
       +memused () {
       +    read t f <<< `grep -E 'Mem(Total|Free)' /proc/meminfo |awk '{print $2}'`
       +    read b c <<< `grep -E '^(Buffers|Cached)' /proc/meminfo |awk '{print $2}'`
       +    mem=`bc <<< "($t - $f - $c - $b)"`
       +
       +    if [ $mem -gt 1048576 ]; then
       +        mem=`bc <<< "scale=2; $mem / 1048576"`
       +        mem="${mem}G"
       +    elif [ $mem -gt 1024 ]; then
       +        mem=`bc <<< "$mem / 1024"`
       +        mem="${mem}M"
       +    fi
       +
       +    echo $mem
       +}
       +
       +# display the memory used in percentage
       +memperc () {
       +    read t f <<< `grep -E 'Mem(Total|Free)' /proc/meminfo |awk '{print $2}'`
       +    read b c <<< `grep -E '^(Buffers|Cached)' /proc/meminfo |awk '{print $2}'`
       +    mem=`bc <<< "100 * ($t - $f - $c - $b) / $t"`
       +
       +    echo "${mem}%"
       +}
       +
       +
       +case $1 in
       +    -h) usage;;
       +    -t) memtotal;;
       +    -u) memused;;
       +     *) memperc;;
       + esac
 (DIR) diff --git a/network b/network
       t@@ -0,0 +1,57 @@
       +#!/bin/sh
       +#
       +# z3bra - (c) wtfpl 2014
       +
       +usage () {
       +    cat <<EOF
       +usage: $(basename $0) [-hicwdu]
       +    -h : print help
       +    -i : print out current interface
       +    -c : return 0 if connected to a network (default)
       +    -w : return 0 if connected over wifi
       +    -d : print out size of downloaded packets
       +    -u : print out size of uploaded packets
       +EOF
       +}
       +
       +# get current interface
       +netint() {
       +    for int in $(ls /sys/class/net); do
       +        if grep -q ${int} /proc/net/route; then
       +            echo ${int}
       +            return 0
       +        fi
       +    done
       +
       +    # no interface up, return loopback
       +    echo "lo"
       +}
       +
       +netstate () {
       +    grep -q $(netint) /proc/net/route && return 0 || return 1
       +}
       +
       +wireless () {
       +    grep -q $(netint) /proc/net/wireless && return 0 || return 1
       +}
       +
       +# get upload/download traffic
       +nettraffic() {
       +    case $1 in
       +        up)     col=10 ;;
       +         *)     col=2  ;;
       +    esac
       +
       +    traffic=$(awk "/$(netint)/ {print \$$col}" /proc/net/dev)
       +
       +    human ${traffic}
       +}
       +
       +case $1 in
       +    -h) usage ;;
       +    -i) netint ;;
       +    -w) wireless ;;
       +    -d) nettraffic down ;;
       +    -u) nettraffic up ;;
       +     *) netstate ;;
       + esac
 (DIR) diff --git a/volume b/volume
       t@@ -1,9 +1,19 @@
        #!/bin/sh
        #
        # z3bra - (c) wtfpl 2014
       -# Manage ALSA Master channel
        
       -test "$1" = "-h" && echo "usage `basename $0` [+|-|!]" && exit 0
       +usage () {
       +    cat <<EOF
       +usage: $(basename $0) [-hsla] [-+!]
       +    -h : print help
       +    -s : print on/off
       +    -l : print the current volume percentage
       +    -a : print both level and state (default)
       +     + : volume +5%
       +     - : volume -5%
       +     ! : toggle mute
       +EOF
       +}
        
        level() {
            amixer get Master | sed -n 's/^.*\[\([0-9]\+%\).*$/\1/p' | uniq
       t@@ -13,12 +23,15 @@ state() {
            amixer get Master | sed -n 's/^.*\[\(o[nf]\+\)]$/\1/p' | uniq
        }
        
       +# print out level and state if no argument is given
        test $# -eq 0 && echo "`level` `state`" && exit 0
        
        case $1 in
       -    +)      amixer set Master 5%+ >/dev/null;;
       -    -)      amixer set Master 5%- >/dev/null;;
       -    level|state) $1;;
       -    !)      amixer set Master toggle >/dev/null;;
       -    *)    amixer set Master $1 >/dev/null;;
       +    -h)     usage ;;
       +    -s)     state ;;
       +    -l)     level ;;
       +     +)     amixer set Master 5%+ >/dev/null;;
       +     -)     amixer set Master 5%- >/dev/null;;
       +     !)     amixer set Master toggle >/dev/null;;
       +     *)     amixer set Master $1 >/dev/null;;
        esac
 (DIR) diff --git a/xgroups b/xgroups
       t@@ -0,0 +1,70 @@
       +#!/bin/sh
       +#
       +# z3bra - (c) wtfpl 2014
       +
       +usage () {
       +    cat <<EOF
       +usage: $(basename $0) [-hta]
       +    -h : print help
       +    -c : print the current group number (default)
       +    -t : print the number of groups
       +    -a : print all groups
       +
       +environment:
       +    GRP_CURRENT : look of current dekstop
       +    GRP_HAZWIN  : look of desktop holding windows
       +    GRP_EMPTY   : look of empty groups
       +    SEPARATOR   : group separator (on the right of each desktop)
       +EOF
       +}
       +
       +# display the current group
       +current () {
       +    xprop -root _NET_CURRENT_DESKTOP | awk '{print $3}'
       +}
       +
       +# display the total number of groups
       +total () {
       +    xprop -root _NET_NUMBER_OF_DESKTOPS | awk '{print $3}'
       +}
       +
       +# print current group and shown groups
       +fullbar () {
       +    cur=$(current)
       +    tot=$(total)
       +    max=$((tot - 1))
       +
       +    # create a list containing the groups holding at least one window
       +    for wid in `xprop -root | sed '/_LIST(WINDOW)/!d;s/.*# //;s/,//g'`; do
       +        # uncomment this "if" statement to show groups only if their windows
       +        # are visible
       +        #if grep -q 'IsViewable' <<< $(xwininfo -id $wid); then
       +            grp=`xprop -id $wid _NET_WM_DESKTOP | awk '{print $3}'`
       +            shown="$shown $grp"
       +        #fi
       +    done
       +
       +    # create a bar containing all desktops
       +    for g in `seq 0 ${max}`; do
       +        if test $g -eq $cur; then         l="${l}${GRP_CURRENT}"
       +        elif grep -q $g <<<"$shown"; then l="${l}${GRP_HAZWIN}"
       +        else                              l="${l}${GRP_EMPTY}"
       +        fi
       +
       +        test ${g} -lt ${max} && l="${l}${SEPARATOR}"
       +    done
       +
       +    echo "$l"
       +}
       +
       +GRP_CURRENT=${GRP_CURRENT:-*}
       +GRP_HAZWIN=${GRP_HAZWIN:-+}
       +GRP_EMPTY=${GRP_EMPTY:--}
       +SEPARATOR=${SEPARATOR:- }
       +
       +case $1 in
       +    -h) usage ;;
       +    -t) total ;;
       +    -a) fullbar ;;
       +     *) current ;;
       + esac