0

I am creating a bash script to get cpu%, pps & incoming kbps

#!/bin/bash

INTERVAL="0.5" # update interval in seconds IFS="enp0s3"

while true do # Read /proc/stat file (for first datapoint) read cpu user nice system idle iowait irq softirq steal guest< /proc/stat # compute active and total utilizations cpu_active_prev=$((user+system+nice+softirq+steal)) cpu_total_prev=$((user+system+nice+softirq+steal+idle+iowait)) sleep $INTERVAL # Read /proc/stat file (for second datapoint) read cpu user nice system idle iowait irq softirq steal guest< /proc/stat # compute active and total utilizations cpu_active_cur=$((user+system+nice+softirq+steal)) cpu_total_cur=$((user+system+nice+softirq+steal+idle+iowait)) # compute CPU utilization (%) cpu_util=$((100*( cpu_active_cur-cpu_active_prev ) / (cpu_total_cur-cpu_total_prev) )) echo "CPU: $cpu_util"

        R4=$(cat /sys/class/net/$IFS/statistics/rx_bytes)
        sleep $INTERVAL
        R5=$(cat /sys/class/net/$IFS/statistics/rx_bytes)
        R8BPS=$(expr $R5 - $R4)
        RKBPS=$(expr $R8BPS / 125)
        echo &quot;IN: $RKBPS&quot;

        R1=$(cat /sys/class/net/$IFS/statistics/rx_packets)
        T1=$(cat /sys/class/net/$IFS/statistics/tx_packets)
        sleep $INTERVAL
        R2=$(cat /sys/class/net/$IFS/statistics/rx_packets)
        T2=$(cat /sys/class/net/$IFS/statistics/tx_packets)
        RBPS=$(expr $R2 - $R1)
        echo &quot;PPS : $RBPS&quot;

done

I am getting a syntax error:

line 11: u  2: syntax error in expression (error token is "2")

Can someone please help me to fix this?

Kusalananda
  • 333,661
ph3ro
  • 378
  • 4
  • 14
  • 3
    Try using a different name for your IFS variable (ifs, for example). The IFS variable is special, and the shell uses it to split any string resulting from unquoted expansions into words. Withthe value of $IFS as you have in the script, the string 0.02 would be split into the two words . and 2 (since $IFS contains 0). – Kusalananda Aug 29 '22 at 10:14
  • Thanks It worked :) – ph3ro Aug 29 '22 at 10:21
  • Ph3ro use lowercase variable names and you are far more likely to avoid unintentionally using reserved variable names such as IFS. Also remember to paste your code into https://shellcheck.net/ to check for errors – Chris Davies Sep 02 '22 at 10:54

1 Answers1

3

The issue comes from the fact that you are using a variable called IFS. The IFS variable happens to be special in any POSIX shell. The shell uses this variable's value as the characters to split the result of unquoted expansions, and it affects the operation of the read utility. By default, this variable contains the three characters space, tab, and newline.

With IFS="enp0s3" and e.g. string=alpha, running echo $string would output al ha since the shell would split the string on the p which is part of $IFS.

The IFS variable also affects how the read utility reads values into variables, splitting the read strings on the characters in $IFS. It is this that I think creates your specific issue since any instance of the digits 0 and 3 would disappear.

To fix this, use another variable name, e.g. ifs. In general, use lower-case variable names to avoid issues like these.

See also:

Kusalananda
  • 333,661