0

I tried converting this function.

R1=`cat /sys/class/net/eth1/statistics/rx_bytes`
T1=`cat /sys/class/net/eth1/statistics/tx_bytes`
sleep $INTERVAL
R2=`cat /sys/class/net/eth1/statistics/rx_bytes`
T2=`cat /sys/class/net/eth1/statistics/tx_bytes`
TBPS=`expr $T2 - $T1`
RBPS=`expr $R2 - $R1`
TKBPS=`expr $TBPS / 1024`
RKBPS=`expr $RBPS / 1024`
echo "TX $1: $TKBPS kb/s RX $1: $RKBPS kb/s"

To this in perl:

my $rx = `cat /sys/class/net/eth1/statistics/rx_bytes`;
my $tx = `cat /sys/class/net/eth1/statistics/tx_bytes`;

my $rx1 = `cat /sys/class/net/eth1/statistics/rx_bytes`;
my $tx1 = `cat /sys/class/net/eth1/statistics/tx_bytes`;

my $tb = $tx1 - $tx;
my $rb = $rx1 - $rx;

my $kbs = $tb / 1024;
my $rbs = $rb / 1024;
sleep 1;
print "$kbs $rbs\n";

Bash script output:

TX eth1: 10 kB/s RX eth1: 16 kB/s
TX eth1: 10 kB/s RX eth1: 15 kB/s
TX eth1: 10 kB/s RX eth1: 16 kB/s
TX eth1: 9 kB/s RX eth1: 14 kB/s

Perl script output:

0.158203125 0.212890625
0.22265625 0.14453125
0.052734375 0.072265625
0 0.072265625
0 0.140625

As you can see its not outputting what I think it should be, any help would be appreciated..

csiu
  • 3,159
  • 2
  • 24
  • 26
jogndogn
  • 13
  • 3

1 Answers1

2

Your bash script output does not match the echo statement in the bash script code you give. There is a difference between bits and bytes, so you either have a typo in your output, or you are missing code from your function.

In any case, expr will return an integer if you divide two integers, while Perl will return a floating-point number if you divide two integers.

If you need an integer result from the Perl script, look into explicitly casting your number arguments with the int keyword. For an example, see: How can I make integer division in Perl OR How can I make my binary search work?

Community
  • 1
  • 1
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • Thank you i fixed the code perfectly, using printf, and forgot to put the sleep between where it gets the data. – jogndogn May 01 '14 at 05:41