1、shell实例
(1)打印九九乘法表
[root@test shell]# vi for-1.sh #!/bin/bashfor i in `seq 9` #变量i取1-9之间的整数do for j in `seq $i`#变量j分别取1-1,1-2,1-3...1-9之间的整数 do echo -n \"$i*$j= `echo $(($i*$j))` \"#不换行输出变量i与变量j相乘的结果 done echo \" \"done
(2)ping整个网段地址,并输出结果到指定文件
[root@test shell]# vi ping.sh #!/bin/bashfor i in `seq 254`do ping -c 3 192.168.0.$i &>/dev/null if [ $? = 0 ]; then echo \"192.168.0.$i up \">>ping.txt else echo \"192.168.0.$i down\" >> ping.txt fidone
(3)抓取指定网口实时流量
[root@test shell]# vi liuliang.sh#!/bin/bashethn=$1while true#每执行一次while true,就会调用true命令。#true用于和其他命令进行逻辑运算。返回状态总是成功;返回值为0。do RX_pre=$(cat /proc/net/dev | grep $ethn | sed \'s/:/ /g\' | awk \'{print $2}\') TX_pre=$(cat /proc/net/dev | grep $ethn | sed \'s/:/ /g\' | awk \'{print $10}\') sleep 1 RX_next=$(cat /proc/net/dev | grep $ethn | sed \'s/:/ /g\' | awk \'{print $2}\') TX_next=$(cat /proc/net/dev | grep $ethn | sed \'s/:/ /g\' | awk \'{print $10}\') clear echo -e \"\\t RX `date +%k:%M:%S` TX\" RX=$((${RX_next}-${RX_pre})) TX=$((${TX_next}-${TX_pre})) if [[ $RX -lt 1024 ]];then RX=\"${RX}B/s\" elif [[ $RX -gt 1048576 ]];then RX=$(echo $RX | awk \'{print $1/1048576 \"MB/s\"}\') else RX=$(echo $RX | awk \'{print $1/1024 \"KB/s\"}\') fi if [[ $TX -lt 1024 ]];then TX=\"${TX}B/s\" elif [[ $TX -gt 1048576 ]];then TX=$(echo $TX | awk \'{print $1/1048576 \"MB/s\"}\') else TX=$(echo $TX | awk \'{print $1/1024 \"KB/s\"}\') fi echo -e \"$ethn \\t $RX $TX \"done[root@test shell]# sh liuliang.sh eth0
个人公众号: