#!/bin/bash

net_dev=$1
OLD_RX=0
OLD_TX=0
TOTAL_RX=0
TOTAL_TX=0

while true; do
  NEW=$(cat /proc/net/dev | grep $net_dev | tr -s ' ' | cut -d' ' -f "3 11")
  NEW_RX=$(echo $NEW | cut -d' ' -f1)
  NEW_TX=$(echo $NEW | cut -d' ' -f2)
  
    if [ $OLD_RX -ne 0 ] && [ $OLD_TX -ne 0 ]; then
      TOTAL_RX=$((TOTAL_RX + NEW_RX - OLD_RX))
      TOTAL_TX=$((TOTAL_TX + NEW_TX - OLD_TX))
    fi
    RX_RATE=$(echo "scale=2; $TOTAL_RX / 1024" | bc)
    TX_RATE=$(echo "scale=2; $TOTAL_TX / 1024" | bc)
    RX_UNIT="KB"
    TX_UNIT="KB"
    if (( $(echo "$RX_RATE >= 1024" |bc -l) )); then
      RX_RATE=$(echo "scale=2; $RX_RATE / 1024" | bc)
      RX_UNIT="MB"
    fi
    if (( $(echo "$TX_RATE >= 1024" |bc -l) )); then
      TX_RATE=$(echo "scale=2; $TX_RATE / 1024" | bc)
      TX_UNIT="MB"
    fi

  printf "\rin: % 9.2f %s\t\tout: % 9.2f %s" $RX_RATE $RX_UNIT $TX_RATE $TX_UNIT
  
  OLD_RX=$NEW_RX
  OLD_TX=$NEW_TX
  
  sleep 1
done
