[tpwd] Fix segfault when exactly one argument given
[tinyapps.git] / traf.sh
blobe04a92953d8d74e49361ccb273ec04cf44889e7a
1 #!/bin/sh
2 ##
3 ## Shows TX/RX over 1sec
4 ## By Michal Nazareicz (mina86/AT/mina86.com)
5 ## & Stanislaw Klekot (dozzie/AT/irc.pl)
6 ## Released to Public Domain
7 ##
8 ## This is part of Tiny Applications Collection
9 ## -> http://tinyapps.sourceforge.net/
12 # usage: ./traf.sh <interface>
13 # <interface> can be a BRE
16 set -e
17 INT="${1-eth0}"
19 # shellcheck disable=SC2120
20 get_traffic () {
21 TX2=0; RX2=0;
22 while read LINE; do
23 expr X"${LINE%%:*}" : X".*\\($INT\\)" >/dev/null || continue
24 # shellcheck disable=SC2086
25 set -- ${LINE#*:}
26 RX2=$(( RX2 + $1 ))
27 TX2=$(( RX2 + $9 ))
28 done </proc/net/dev
31 # shellcheck disable=SC2119
32 get_traffic
34 while sleep 1; do
35 TX1=$TX2
36 RX1=$RX2
37 # shellcheck disable=SC2119
38 get_traffic
40 TX=$(( TX2 - TX1 ))
41 RX=$(( RX2 - RX1 ))
43 PT=
44 PR=
45 I=0
46 while [ "$I" -lt 15360 ]; do
47 if [ "$I" -lt "$TX" ]; then PT="#$PT"; else PT=" $PT"; fi
48 if [ "$I" -lt "$RX" ]; then PR="$PR#"; else PR="$PR "; fi
49 I=$(( I + 512 ))
50 done
51 if [ "$TX" -gt 2048 ]; then TX="$(( TX / 1024 )) K"; fi
52 if [ "$RX" -gt 2048 ]; then RX="$(( RX / 1024 )) K"; fi
53 # printf "%s < %6s | %6s > %s\n" "$PT" "$TX" "$RX" "$PR"
54 printf "%6s %s | %s %6s\n" "$TX" "$PT" "$PR" "$RX"
56 # PROGRESS=""
57 # I=0
58 # while [ $I -lt $TX -o $I -lt $RX ]; do
59 # if [ $I -gt $TX ]; then
60 # PROGRESS="$PROGRESS'"
61 # elif [ $I -gt $RX ]; then
62 # PROGRESS="$PROGRESS."
63 # else
64 # PROGRESS="$PROGRESS:"
65 # fi
66 # I=$(( $I + 512 ))
67 # done
68 # if [ $TX -gt 2048 ]; then TX="$(( $TX / 1024 )) K"; fi
69 # if [ $RX -gt 2048 ]; then RX="$(( $RX / 1024 )) K"; fi
70 # printf "< %6s > %6s %s\n" "$TX" "$RX" "$PROGRESS"
71 done