Solved Show Speed Rate Written in Shell

Hello,

I am trying to make a script that can show the data transfer rate just like what you got when running the command systat -ifstat 1
I gather some information on the internet but the result my script shows has nothing to do with reality.
In order to simplified things for now I am only focus on the incoming traffic side, when I'll understand the logic and where my error is I'll do the rest after.

According to this site, the formula is: Speed = amount of data / transfer time
_ the amount of data collected is obtained by using the nestat command, the result is in bytes which is converted to Megabytes to be more human readable.
_ transfer time is one second because it is simpler(divided by one is like doing nothing), so the while loop runs the operation every second.

In theory it looks like an easy thing to do, but still I am struggling, the results are far from okay.
My incoming internet speed is around 1.5MB/s the script shows something like 2749.

I am pretty bad at math so I probably missed an important point here, can you guys guide me on this?
I have no merit for the following script I've been largely inspired by this video on youtube and this question on stackoverflow, I just try to understand and convert it to FreeBSD for a personal usage.

Thank you.

Bash:
tmpfile=$(mktemp)

while true
do

# store previous result
bytes_previous=$(cat "$tmpfile")

# get current bytes value
bytes_current=$(netstat -4 -i -b -I igb0 | awk '{print $8}' | sed 1d)

# get difference in MegaBytes
bytes_diff=$(( (bytes_current - bytes_previous) / (1024*1024) ))

echo "$bytes_diff" | tee "$tmpfile"

# repeat the operation every second
sleep 1

done
 
You're writing the changed number of bytes to $tmpfile, then use that as the 'previous' counter. You need to save 'bytes_current' to 'bytes_previous', not 'bytes_diff'. You have to diff the absolute values of bytes_current and bytes_previous to determine the amount it changed.
 
Ho yeah it works better now !
When I read your comment it seems so obvious but damn I couldn't figure it by myself.
I'll post later the final script when it'll work totally as I want.

Thank you SirDice for your clear explanation !!!
:)
 
Code:
netstat -w 1 -I igb0 |grep --line-buffered -v t |{ while read ip ie id ib op oe ob oc;do echo IN_B/s $ib OUT_B/s $ob;done;}
 
# get difference in MegaBytes bytes_diff=$(( (bytes_current - bytes_previous) / (1024*1024) ))

This is integer arithmetic, so if (current - previous) is anywhere between say 1MiB and (2MiB - 1), divided by 1Mi = 1, and if (1MiB - 1), the integer result is 0 ...

So this needs real number maths; and is an ideal job for bc(1).

Code:
result=`echo "scale=3; ($current - $previous) / (1024*1024)" | bc`

So if current-previous was 1234567 then
$result is 1.177
and for difference 1048575
$result is .999
 
smithi
Thank you.
Yes I came to the same conclusion and like you I also used bc , it is also possible to convert the result in Kilo instead.

For now I am still working on the script, not that it will be astonish but I am just trying things out.
 
For now I am still working on the script, not that it will be astonish but I am just trying things out.
It's a nice little project to learn scripting. You're going to run into certain pitfalls (like the implied integer conversion for example), there's another pitfall but I'm not going to warn you, you should find it on your own ;) It's a good way to do some hands-on learning (the best way in my opinion).

Oh, and a good website is https://www.grymoire.com/Unix/Sh.html
 
Back
Top