sh/bash/dash/ksh/zsh等Shell脚本
-
YeLee
- 论坛版主
- 帖子: 26406
- 注册时间: 2008-08-13 8:48
- 系统: Fundu i64
- 来自: 东海硇州,一双管钥。
-
联系:
#1
帖子
由 YeLee » 2012-05-02 13:41
代码: 全选
#! /bin/bash
nic=$1
netdev=/proc/net/dev
if [ $# != 1 ] ; then
echo "Usage : $0 NIC"
exit 1
fi
if [ -z "$(grep $nic: $netdev)" ];then
echo "Invalid NIC"
exit 1
fi
in_orig=$(grep $nic $netdev|sed "s/$nic://"|awk '{ print $1 }')
out_orig=$(grep $nic $netdev|sed "s/$nic://"|awk '{ print $9 }')
while true
do
sleep 1
in=$(grep $nic $netdev|sed "s/$nic://"|awk '{ print $1 }')
out=$(grep $nic $netdev|sed "s/$nic://"|awk '{ print $9 }')
in_diff=$(echo $in $in_orig|awk '{printf "%5.2f", ($1-$2)/1024}')
out_diff=$(echo $out $out_orig|awk '{printf "%5.2f", ($1-$2)/1024}')
in_total=$(echo $in|awk '{printf "%6.2f", $1/1024/1024}')
out_total=$(echo $out|awk '{printf "%6.2f", $1/1024/1024}')
echo "$nic:IN ${in_diff}K OUT ${out_diff}K TOTALIN ${in_total}M TOTALOUT ${out_total}M"
in_orig=${in}
out_orig=${out}
done
修改了一下,应该差不多了。
-
eexpress
- 帖子: 58428
- 注册时间: 2005-08-14 21:55
- 来自: 长沙
#2
帖子
由 eexpress » 2012-05-02 15:14
bash当lisp写啊。这么多重()。
那些变量,只要运算,都是整数了吧。
估计只能在echo里面,写bc。echo "312.4/40"|bc -l。当然awk可以。
换perl最好。
● 鸣学
-
jtshs256
- 帖子: 22323
- 注册时间: 2010-07-19 21:41
- 系统: OS X
#3
帖子
由 jtshs256 » 2012-05-02 15:18
自带的当然没小数,我都直接*100 再加 . 的
躺平
-
麦斯特
- 帖子: 1034
- 注册时间: 2005-03-28 0:00
- 系统: Gentoo x64
- 来自: ☸我佛山人
#4
帖子
由 麦斯特 » 2012-05-02 15:44
伟大的神啊,这种小菜鸟写出的东西乃就不要喷了吧。
Je ne suis pas d'accord avec ce que vous dites, mais je me battrai jusqu'à la mort pour que vous ayez le droit de le dire.
-
lilydjwg
- 论坛版主
- 帖子: 4258
- 注册时间: 2009-04-11 23:46
- 系统: Arch Linux
-
联系:
#5
帖子
由 lilydjwg » 2012-05-02 16:35
bash 不支持浮点数么?
-
YeLee
- 论坛版主
- 帖子: 26406
- 注册时间: 2008-08-13 8:48
- 系统: Fundu i64
- 来自: 东海硇州,一双管钥。
-
联系:
#6
帖子
由 YeLee » 2012-05-02 20:00
-
jtshs256
- 帖子: 22323
- 注册时间: 2010-07-19 21:41
- 系统: OS X
#7
帖子
由 jtshs256 » 2012-05-02 20:04
lss 用的 zsh?……
躺平
-
枫叶饭团
- 帖子: 14683
- 注册时间: 2010-06-16 1:05
- 系统: Mac OS X
- 来自: Tencent
-
联系:
#8
帖子
由 枫叶饭团 » 2012-05-02 20:30
jtshs256 写了:lss 用的 zsh?……
听说的是
-
lilydjwg
- 论坛版主
- 帖子: 4258
- 注册时间: 2009-04-11 23:46
- 系统: Arch Linux
-
联系:
#9
帖子
由 lilydjwg » 2012-05-02 20:36
枫叶饭团 写了:jtshs256 写了:lss 用的 zsh?……
听说的是
嗯,支持浮点数的。我记得曾经看到哪个软件在某个版本增加了浮点数的支持,还以为是 bash 呢,现在想起是 Vim。
-
YeLee
- 论坛版主
- 帖子: 26406
- 注册时间: 2008-08-13 8:48
- 系统: Fundu i64
- 来自: 东海硇州,一双管钥。
-
联系:
#10
帖子
由 YeLee » 2012-05-02 23:07
代码: 全选
diff -u nicmonitor.orig nicmonitor
--- nicmonitor.orig 2012-05-02 19:49:26.253538669 +0800
+++ nicmonitor 2012-05-02 21:45:00.306720215 +0800
@@ -16,10 +16,10 @@
sleep 1
in=$(grep $nic $netdev|sed "s/$nic://"|awk '{ print $1 }')
out=$(grep $nic $netdev|sed "s/$nic://"|awk '{ print $9 }')
- in_diff=$(echo $in $in_orig|awk '{printf "%5.2f", ($1-$2)/1024}')
- out_diff=$(echo $out $out_orig|awk '{printf "%5.2f", ($1-$2)/1024}')
- in_total=$(echo $in|awk '{printf "%6.2f", $1/1024/1024}')
- out_total=$(echo $out|awk '{printf "%6.2f", $1/1024/1024}')
+ in_diff=$(awk 'BEGIN{printf "%5.2f", '"$[in-in_orig]"'/1024}')
+ out_diff=$(awk 'BEGIN{printf "%5.2f", '"$[out-out_orig]"'/1024}')
+ in_total=$(awk 'BEGIN{printf "%6.2f", '"$in"'/1024/1024}')
+ out_total=$(awk 'BEGIN{printf "%6.2f", '"$out"'/1024/1024}')
echo "$nic:IN ${in_diff}K OUT ${out_diff}K TOTALIN ${in_total}M TOTALOUT ${out_total}M"
in_orig=${in}
out_orig=${out}
无视脚本大婶,再修改一下。

-
ChenFengyuan
- 帖子: 770
- 注册时间: 2008-03-23 0:39
#11
帖子
由 ChenFengyuan » 2012-05-03 16:37
eexpress 写了:bash当lisp写啊。这么多重()。
那些变量,只要运算,都是整数了吧。
估计只能在echo里面,写bc。echo "312.4/40"|bc -l。当然awk可以。
换perl最好。
哪里多了,好像一行才一对()

-
YeLee
- 论坛版主
- 帖子: 26406
- 注册时间: 2008-08-13 8:48
- 系统: Fundu i64
- 来自: 东海硇州,一双管钥。
-
联系:
#12
帖子
由 YeLee » 2012-05-04 13:33
代码: 全选
diff -u nicmonitor.orig nicmonitor
--- nicmonitor.orig 2012-05-02 21:45:00.306720215 +0800
+++ nicmonitor 2012-05-04 13:24:34.221711182 +0800
@@ -20,7 +20,11 @@
out_diff=$(awk 'BEGIN{printf "%5.2f", '"$[out-out_orig]"'/1024}')
in_total=$(awk 'BEGIN{printf "%6.2f", '"$in"'/1024/1024}')
out_total=$(awk 'BEGIN{printf "%6.2f", '"$out"'/1024/1024}')
- echo "$nic:IN ${in_diff}K OUT ${out_diff}K TOTALIN ${in_total}M TOTALOUT ${out_total}M"
+# echo "$nic:IN ${in_diff}K OUT ${out_diff}K TOTALIN ${in_total}M TOTALOUT ${out_total}M"
+ tput sc
+# echo -n "$nic:IN ";tput setf 4;echo -n ${in_diff}K;tput sgr0;echo -n " OUT ";tput setf 4;echo -n ${out_diff}K;tput sgr0;echo -n " TOTALIN ";tput setf 4;echo -n ${in_total}M;tput sgr0;echo -n " TOTALOUT ";tput setf 4;echo -n ${out_total}M;tput sgr0
+ echo -n "$nic:IN ${in_diff}K OUT ${out_diff}K TOTALIN ${in_total}M TOTALOUT ${out_total}M"
+ tput rc
in_orig=${in}
out_orig=${out}
done
本来想加点很邪恶的东西进去的,但发现出了点问题,还是有空再折腾吧。

-
YeLee
- 论坛版主
- 帖子: 26406
- 注册时间: 2008-08-13 8:48
- 系统: Fundu i64
- 来自: 东海硇州,一双管钥。
-
联系:
#13
帖子
由 YeLee » 2012-05-04 16:43
代码: 全选
无高亮刷屏
echo "$nic:IN ${in_diff}K OUT ${out_diff}K TOTALIN ${in_total}M TOTALOUT ${out_total}M"
高亮刷屏
echo -e "$nic:IN \e[1;31m${in_diff}K\e[0m OUT \e[1;31m${out_diff}K\e[0m TOTALIN \e[1;31m${in_total}M\e[0m TOTALOUT \e[1;31m${out_total}M\e[0m"
无高亮单行输出
echo -ne "\e[s$nic:IN ${in_diff}K OUT ${out_diff}K TOTALIN ${in_total}M TOTALOUT ${out_total}M\e[u"
高亮单行输出
echo -ne "\e[s$nic:IN \e[1;31m${in_diff}K\e[0m OUT \e[1;31m${out_diff}K\e[0m TOTALIN \e[1;31m${in_total}M\e[0m TOTALOUT \e[1;31m${out_total}M\e[0m\e[u"
今天下午查了点资料,总算弄清楚这个问题了。

-
liu滔
- 帖子: 7214
- 注册时间: 2010-10-09 23:01
#14
帖子
由 liu滔 » 2012-05-09 22:30
YeLee真厉害

-
YeLee
- 论坛版主
- 帖子: 26406
- 注册时间: 2008-08-13 8:48
- 系统: Fundu i64
- 来自: 东海硇州,一双管钥。
-
联系:
#15
帖子
由 YeLee » 2012-07-16 21:51
晕,发现加了控制代码之后,数值出现了很大的偏差……假如太精确的话,那效率确实不敢恭维。
