0

I have made a bash script to monitor my network traffic every 10 sec using ifconfig. It must be ifconfig else I would have used a different tool.

My issue is because my counter is so high number I have to use expr to do the calculation, however expr is causing me some issues.

#!/bin/bash
#run for how many seconds

savefile=/root/eth0.csv
timer=10

echo "EPOCH,Interface,TX,RX,ChangeTx,ChangeRX" >> $savefile

END=8640
x=$END
while [ $x -gt 0 ]; do

ETH0RX=`ifconfig | grep eth0 -A8 | grep "RX bytes" | tr -s ' ' | cut -d':' -f2 | cut -d ' ' -f1`
ETH0TX=`ifconfig | grep eth0 -A8 | grep "RX bytes" | tr -s ' ' | cut -d':' -f3 | cut -d ' ' -f1`

#ETH0 RX
ETH0RXcurrentvalue=$ETH0RX
ETH0RXchange=$(expr $ETH0RXcurrentvalue - $ETH0RXpreviousvalue)
ETH0RXpreviousvalue=$ETH0RXcurrentvalue

#ETH0 TX
ETH0TXcurrentvalue=$ETH0TX
ETH0TXchange=$(expr $ETH0TXcurrentvalue - $ETH0TXpreviousvalue)
ETH0TXpreviousvalue=$ETH0TXcurrentvalue

epoch=`date +%s`

echo $epoch,ETH0,$ETH0TX,$ETH0RX,$(($ETH0RXchange*8)),$(($ETH0TXchange*8)) >> $savefile

sleep $timer

x=$(($x-1))
done;

My error is line 33: arithmetic syntax error (which is done; ). The script works fine if there is no expr, however due to big numbers I need to use expr (I cannot use bc).

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Svan
  • 41
  • 6
  • You need to assign `ETH0RXpreviousvalue` and `ETH0TXpreviousvalue` to initial values (maybe 0). Alternatively you can say `ETH0RXchange=$((ETH0RXcurrentvalue - ETH0RXpreviousvalue))` which is better than using `expr`. – tshiono Nov 21 '19 at 09:07
  • I have to expr because my number is 122121212112211221 and the normal match cannot do 122121212112211221 - 122121212112211221 as is longer than the 4 bits, this is why I use the expr – Svan Nov 21 '19 at 09:16
  • Have you tried my code anyway? Bash is using signed 64-bit integers. – tshiono Nov 21 '19 at 09:28
  • @tshiono, your code is what I used before and it works, it just when the counter reaches a long number the result is always 0 so for a period of time i have no values due to the counter cannot do the calculation, this is why i'm looking at expr, else this would have worked just fine. – Svan Nov 21 '19 at 09:34
  • @Svan: The line `done;` does not contain any arithmetic expression, so this can't be the offending line. You do arithmetic in the `while` expression (which is not wrong, but could be better written as `while (( x > 0 ))`), in the increment part *before* the `done`, which also looks fine (but could be better written as `((x=x-1))`), so the problem must be in the `echo` line. What is the value of `ETH0RXchange` when the error occurs? – user1934428 Nov 21 '19 at 10:10

1 Answers1

0

I have fixed the issue,

As I'm trying to do an

expr

and then later on the script I do another calculation the

expr

continuously giving error because of the other line where the calculation was via echo.

so I fixed using the code below.

ETH0RXcurrentvalue=$ETH0RX
ETH0RXchange=$(expr "$ETH0RXcurrentvalue" - "$ETH0RXpreviousvalue")
ETH0RXpreviousvalue=$ETH0RXcurrentvalue
ETH0RXchange1=$(expr "$ETH0RXchange" \* "8")
echo $ETH0RXchange1
echo $ETH0TXchange1


echo $epoch,ETH0,$ETH0TX,$ETH0RX,$ETH0RXchange1,$ETH0TXchange1 >> $savefile
Svan
  • 41
  • 6