The officially official Devuan Forum!

You are not logged in.

#1 2020-12-06 12:41:13

dice
Member
Registered: 2020-11-22
Posts: 559  
Website

dwm statusbar bash script

Managed to hack this together today from various sources, if you use it you would have to play around with the bash command functions.

What i like about this script is ive found a way to maintain timed loops inside the while loop itself using let. I would love any feedback to see if this is a viable script free from errors.

#!/bin/bash

get_cpu (){
	printf "Cpu:"
	top -bn1 | awk 'NR==3 {print $2}'
	
}

get_disk (){
	printf "| Disk:"
	df -h | awk 'NR==4 { print $3" / "$2}'
}

get_mem (){
	printf "| Mem:"
	free -h | awk 'NR==2 {print $3" / "$2}'
}

get_temp (){
	printf "| Temps:"
	sensors | awk '/Core/ { printf substr($3,2,2)"°C " }'
}

get_time (){
	printf "| Time:"
	date +"%R"
}

let loop=0
while true; do
   if [[ $loop%60 -eq 0 ]]; then
      Time=$(get_time)
   fi
   
   if [[ $loop%300 -eq 0 ]]; then
      Disk=$(get_disk)
      let loop=0  #this prevents an eventual overflow
   fi
   
   if [[ $loop%8 -eq 0 ]]; then
      Temp=$(get_temp)
   fi
   
   if [[ $loop%3 -eq 0 ]]; then
      Mem=$(get_mem)
   fi
      if [[ $loop%2 -eq 0 ]]; then
      Cpu=$(get_cpu)
   fi
   
   xsetroot -name "$Cpu $Temp $Disk $Mem $Time"
   let loop=$loop+1
   sleep 1
   
done

Update: ran this script through shell check, interesting diff.
https://www.shellcheck.net/

#!/bin/bash

get_cpu (){
	printf "Cpu:"
	top -bn1 | awk 'NR==3 {print $2}'
	
}

get_disk (){
	printf "| Disk:"
	df -h | awk 'NR==4 { print $3" / "$2}'
}

get_mem (){
	printf "| Mem:"
	free -h | awk 'NR==2 {print $3" / "$2}'
}

get_temp (){
	printf "| Temps:"
	sensors | awk '/Core/ { printf substr($3,2,2)"°C " }'
}

get_time (){
	printf "| Time:"
	date +"%R"
}

(( loop=0 ))
while true; do
   if [[ $loop%60 -eq 0 ]]; then
      Time=$(get_time)
   fi
   
   if [[ $loop%300 -eq 0 ]]; then
      Disk=$(get_disk)
      (( loop=0 ))  #this prevents an eventual overflow
   fi
   
   if [[ $loop%8 -eq 0 ]]; then
      Temp=$(get_temp)
   fi
   
   if [[ $loop%3 -eq 0 ]]; then
      Mem=$(get_mem)
   fi
      if [[ $loop%2 -eq 0 ]]; then
      Cpu=$(get_cpu)
   fi
   
   xsetroot -name "$Cpu $Temp $Disk $Mem $Time"
   (( loop=loop+1 ))
   sleep 1
   
done

Last edited by dice (2020-12-06 15:05:55)

Offline

#2 2020-12-24 10:27:24

dice
Member
Registered: 2020-11-22
Posts: 559  
Website

Re: dwm statusbar bash script

created a network iface script for this statusbar script. This kind of similar to how conky does it with -

gw_iface Displays the default route's interface or "multiple"/"none" accordingly.

I was told over at stackoverflow i shouldnt be using ifconfig for something as simple as this, which i think is ridiculous, if i wanted to do fancy stuff with the networks i would use iproute2 but for this, not needed. If it is old and deprecated why is it still in the kernel?

This will print on my statusbar as follows,

  • If usb net connected = usb0

  • If wlan0 connected = wlan0

  • If both usb and wlan0 connected =multiple

  • If no network = not connected

#!/usr/bin/env bash

a=$(ifconfig | grep -ow "wlan0\|usb0")
b=$'wlan0\nusb0'
c="%printf '$b'"

if      [[ "$a" = "wlan0" ]] ; then
        printf "wlan0"

elif    [[ "$a" = "usb0" ]] ; then
        printf "usb0"

elif    [[ "$a" =~ ["$c"] ]] ; then
        printf "multiple"

else
        printf "not connected"
fi

Last edited by dice (2020-12-24 10:36:42)

Offline

#3 2020-12-24 15:57:18

Head_on_a_Stick
Member
From: London
Registered: 2019-03-24
Posts: 3,125  
Website

Re: dwm statusbar bash script

dice wrote:

If it is old and deprecated why is it still in the kernel?

It's not in the kernel, ifconfig is a(n obsolete) userspace utility.

And anyway what's wrong with

a=$(ip l | grep -ow "wlan0\|usb0")

Off topic for this thread but you should check out slstatus — pure C ftw!


Brianna Ghey — Rest In Power

Offline

#4 2020-12-24 18:30:16

manyroads
Member
From: 20 Jan 2021 ~ "Free at last"
Registered: 2020-08-18
Posts: 73  
Website

Re: dwm statusbar bash script

I'd echo the vote for slstatus.... to my small mind it is by far and away the best option.


Pax vobiscum,
Mark Rabideau - ManyRoads
i3wm, bspwm, dkwm, dwm, hlwm, sway, openbox on Sid/ ceres ~ Linux #449130
"For every complex problem there is an answer that is clear, simple, and wrong." -- H. L. Mencken

Offline

#5 2020-12-25 01:00:12

dice
Member
Registered: 2020-11-22
Posts: 559  
Website

Re: dwm statusbar bash script

Head_on_a_Stick wrote:
dice wrote:

If it is old and deprecated why is it still in the kernel?

It's not in the kernel, ifconfig is a(n obsolete) userspace utility.

And anyway what's wrong with

a=$(ip l | grep -ow "wlan0\|usb0")

Off topic for this thread but you should check out slstatus — pure C ftw!

In regards to why is it in the kernel, i should have referenced the answer i got from stackoverflow. I know it is part of the net-tools package so i was confused about this quote:

That said, ifconfig shouldn't be used at all on Linux either; it hasn't been maintained by the upstream kernel team for well over a decade now

In regards to ip, i have some issues with ip l , the difference being that ip l shows ifaces even if they are down whereas ifconfig and netstat does not. So this would make the script only output multiple if ip was used.

Here is ip l on my machine with usb0 and wlan0, with only wlan0 up.

ifconfig
lo: flags=73<UP,LOOPBACK,RUNNING>
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>
netstat -i
Kernel Interface table
Iface      MTU    RX-OK RX-ERR RX-DRP RX-OVR    TX-OK TX-ERR TX-DRP TX-OVR Flg
lo       65536        0      0      0 0             0      0      0      0 LRU
wlan0     1500      810      0      0 0           724      0      0      0 BMRU
ip l
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000
4: usb0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000

I have used slstatus in the past, i may revert back to it again, although this endeavor was just to see if i could create shell scripts to pipe to dwm statusbar instead, which i have accomplished.

Last edited by dice (2020-12-25 01:14:55)

Offline

#6 2020-12-25 01:36:54

dice
Member
Registered: 2020-11-22
Posts: 559  
Website

Re: dwm statusbar bash script

head on a stick, I could use ip but have to tack on sort like so.

ip route | grep -ow 'wlan0\|usb0' | sort -u
usb0
wlan0

otherwise without sort

ip route | grep -ow 'wlan0\|usb0'
wlan0
wlan0
usb0

Offline

#7 2020-12-25 08:48:14

Head_on_a_Stick
Member
From: London
Registered: 2019-03-24
Posts: 3,125  
Website

Re: dwm statusbar bash script

dice wrote:

ip l shows ifaces even if they are down whereas ifconfig and netstat does not. So this would make the script only output multiple if ip was used.

Ah yes, of course. But ifconfig shows all interfaces that are up, which includes interfaces which are not connected.

How about this:

ip r | awk '/^default/{print $5}'

^ That shows the name of the interface currently connected.


Brianna Ghey — Rest In Power

Offline

#8 2020-12-25 10:21:38

dice
Member
Registered: 2020-11-22
Posts: 559  
Website

Re: dwm statusbar bash script

Head_on_a_Stick wrote:
dice wrote:

ip l shows ifaces even if they are down whereas ifconfig and netstat does not. So this would make the script only output multiple if ip was used.

Ah yes, of course. But ifconfig shows all interfaces that are up, which includes interfaces which are not connected.

How about this:

ip r | awk '/^default/{print $5}'

^ That shows the name of the interface currently connected.

I suppose the default gateway is the way to go, so if wlan0 is connected first and then usb0, the default is always going to be wlan0 and vice versa, would that be true?. I wonder how conky does the gw_iface object though.

In regards to ifconfig, atleast in my case on devuan beowulf, if wlan0 is not connected it wont show up in ifconfig, only lo. where ip l all interfaces are shown regardless if they are up or down.

Last edited by dice (2020-12-25 10:26:46)

Offline

#9 2020-12-25 15:45:09

Head_on_a_Stick
Member
From: London
Registered: 2019-03-24
Posts: 3,125  
Website

Re: dwm statusbar bash script

dice wrote:

if wlan0 is connected first and then usb0, the default is always going to be wlan0 and vice versa, would that be true?

Yes, I think you're right.

Another alternative:

ip link | awk '/UP /{print $2}'

Brianna Ghey — Rest In Power

Offline

#10 2021-04-15 13:11:46

Ogis1975
Member
Registered: 2017-04-21
Posts: 307  
Website

Re: dwm statusbar bash script

dice wrote:

What i like about this script is ive found a way to maintain timed loops inside the while loop itself using let. I would love any feedback to see if this is a viable script free from errors.

Thank you for this script. Works flawlessly and very well !

2021-04-15-160329-1920x1080-scrot.png


What economists call over-production is but a production that is above the purchasing power of the worker, who is reduced to poverty by capital and state.
            ----+- Peter Kropotkin -+----

Offline

#11 2021-04-15 13:42:30

dice
Member
Registered: 2020-11-22
Posts: 559  
Website

Re: dwm statusbar bash script

Thanks Ogis, glad it worked for you. smile

Offline

#12 2021-04-15 17:00:12

Ogis1975
Member
Registered: 2017-04-21
Posts: 307  
Website

Re: dwm statusbar bash script

dice wrote:

Thanks Ogis, glad it worked for you. smile

Yes. This script works great in Debian 10. I'll try it tomorrow on Devuan 3.1 smile


What economists call over-production is but a production that is above the purchasing power of the worker, who is reduced to poverty by capital and state.
            ----+- Peter Kropotkin -+----

Offline

Board footer