nnscripts

no-nonsense shell script colection for Linux-based environments
git clone git://git.luxferre.top/nnscripts.git
Log | Files | Refs | README | LICENSE

nnstatus-stdout.sh (2378B)


      1 #!/bin/sh
      2 # nnstatus-stdout.sh: no-nonsense status line script (Linux-specific)
      3 # outputs a single line to stdout, it's up to other scripts what to do with it
      4 # only depends on awk (I recommend nawk/one-true-awk) to work with POSIX shells
      5 # and amixer to work with sound and iw to work with wireless networks
      6 # created by Luxferre in 2024, released into public domain
      7 
      8 # configure some parameters here
      9 AWK_CMD=nawk # your awk engine
     10 DELIM=' | ' # delimiter between the fields 
     11 SLEEP_INTERVAL=2 # data refresh interval (in seconds)
     12 BATDEV='BAT0' # battery device indicator, if applicable (unset to disable)
     13 MIXER_CHAN='Master' # ALSA channel to display
     14 
     15 BATDIR="/sys/class/power_supply/$BATDEV"
     16 
     17 # helper functions
     18 
     19 cpustat() { # returns: Idle NonIdle
     20   $AWK_CMD 'NR==1{printf("%u %u", $5+$6, $2+$3+$4+$7+$8+$9)}' /proc/stat
     21 }
     22 
     23 ramstat() { # print used RAM
     24   $AWK_CMD '/^MemTotal:/{mt=$2}/^Buffers:/{mb=$2}/^MemFree:/{mf=$2}\
     25     /^Cached:/{mc=$2}/^Shmem:/{ms=$2}/^SReclaimable:/{msr=$2}\
     26     END{d=mt-mf-mc-msr-mb+ms;if(d<1048576) printf("RAM %4.0fM",d/1024);\
     27     else printf("RAM %4.2fG",d/1048576)}' /proc/meminfo
     28 }
     29 
     30 batstat() { # print battery status
     31   local bstatus="$(cat $BATDIR/status)"
     32   local bpercent="$(cat $BATDIR/capacity)"
     33   local cstatus="CHG" # charging by default
     34   [ "$bstatus" = "Discharging" ] && cstatus="BAT"
     35   printf "%s %03u%%" "$cstatus" "$bpercent"
     36 }
     37 
     38 volstat() {
     39   amixer sget $MIXER_CHAN | $AWK_CMD 'BEGIN{acc=0;c=0;con=1}\
     40     /\[[0-9][0-9]*\%\]/{for(i=1;i<=NF;i++){\
     41     if($i ~ /\[[0-9][0-9]*\%\]/){acc+=int(substr($i,2));c++}\
     42     if($i ~ /\[off\]/){con=0}\
     43     }}END{printf(con?"VOL %03u%%":"VOL OFF ",int(c>0?acc/c:acc))}'
     44 }
     45 
     46 wlanstat() {
     47   local ssid="$(iw dev $1 link | $AWK_CMD '/SSID:/{print $2}')"
     48   printf "%s: " "$1"
     49   [ -z "$ssid" ] && printf "None" || printf "%s" "$ssid" 
     50 }
     51 
     52 CPUFILE='/tmp/nnstatus.cpu'
     53 [ -e "$CPUFILE" ] && PREVSTAT="$(cat $CPUFILE)" || PREVSTAT="$(cpustat)"
     54 
     55 # you can reorder the calls here
     56 
     57 # wifi
     58 wlanstat wlan0
     59 # ram
     60 printf "%s" "$DELIM"
     61 ramstat
     62 # cpu
     63 printf "%s" "$DELIM"
     64 CURSTAT="$(cpustat)"
     65 printf "%s %s" "$PREVSTAT" "$CURSTAT" | $AWK_CMD 'NR==1{t=$3+$4-$2-$1;i=$3-$1;printf("CPU %03d%%",100*(t-i)/t)}'
     66 echo "$CURSTAT" > $CPUFILE
     67 # battery
     68 [ -n "$BATDEV" ] && [ -d "$BATDIR" ] && printf "%s" "$DELIM" && batstat
     69 # volume
     70 printf "%s" "$DELIM"
     71 volstat
     72 # clock
     73 printf "%s\n" "$DELIM$(date +'%a %b %d %H:%M')"
     74