My bash skills are near to nil. I came up with a script to monitor the data transfer speed of my wifi interface.
#!/bin/bash
rx_old=0
tx_old=0
speed() {
read rx_bytes < /sys/class/net/wlp9s0f0/statistics/rx_bytes
read tx_bytes < /sys/class/net/wlp9s0f0/statistics/tx_bytes
drx=$(((rx_bytes - rx_old) / 1024))
dtx=$(((tx_bytes - tx_old) / 1024))
rx_old=$rx_bytes
tx_old=$tx_bytes
echo "$drx KB/s $dtx KB/s"
}
while true;
do
echo $(speed)
sleep 1
done
This is not working, I mean the variable rx_old and tx_old does not seem to update. I think I am not able to change those variables from inside the function. Can anybody give me some insight on this?
rx_bytes=$(cat /sys/class...), and instead ofecho $(speed), just typespeed.speedin a subshell, which does not update the old variables.speedmanipulate variables in the current shell (not in a subshell), including a variable named (e.g.)outcome. The function itself may be silent. Then you simply callspeed, this updates the variables. You retrieve the result withxsetroot -name "$outcome".