shell脚本实例解析

shell脚本实例解析,这些实例都是前辈博客中的,小菜我拿来参考和理解。 网址:http://hi.baidu.com/zzztou/blog/item/fc4b537ea2afd23e0cd7daeb.html
1.删除b文件中和a文件相同的内容;按照行为单位,下例“内容相同”的含义是
a文件中一行正好匹配b文件的一行,则称为“内容相同”,于是删除b文件的该行

#!/bin/bash

for file in `cat a | cut -f1- d.`
do
    sed -i '/'$file'/ d' b
done

关于cut命令,http://blog.csdn.net/Frozen_fish/article/details/2260804;说的很好,很易于理解。

2.根据文件dir.list每一行的前四个字符创建二级目录
mkdir命令的-p选项,同--parents,
需要时创建目标目录的上层目录,但即使这些目录已存在也不当作错误处理

#!/bin/bash

for dir in `cat dir.list`
do
    dir1=`echo $dir | cut -c1-2`
    dir2=`echo $dir | cut -c3-4`
    if [ ! -d "$dir1/$dir2" ]; then
        mkdir -p "$dir1/$dir2"
    fi
done
删除这些刚刚创建的目录,
!/bin/bash

for dir in `cat dir.list`
do
    dir1=`echo $dir|cut -c1-2`
    if [ -d "$dir1" ];then
        rm -r "$dir1"
    fi
done
dir.list文件内容:
abcdefg
bcdefga
cdefgab
defgabc

3.查看网卡流量,以太网卡eth0,注意分析ifconfig eth0
sed的p命令是打印print,-n选项取消自动打印模式空间

date命令要注意,各种选项。
#!/bin/bash
#netflood
#Ajian
while : ; do
        time=`date +%m"-"%d" "%k":"%M`
    echo time=$time
        day=`date +%m"-"%d`
    echo day=$day
        rx_before=`ifconfig eth0|sed -n "9"p|awk '{print $1}'|cut -d: -f2`
    echo rx_before=$rx_before
        tx_before=`ifconfig eth0|sed -n "9"p|awk '{print $4}'|cut -d: -f2-`
    echo tx_before=$tx_before
        sleep 2
        rx_after=`ifconfig eth0|sed -n "9"p|awk '{print $1}'|cut -d: -f2`
    echo rx_after=$rx_after
        tx_after=`ifconfig eth0|sed -n "9"p|awk '{print $4}'|cut -d: -f2`
    echo tx_after=$tx_after
        rx_result=$[(rx_after-rx_before)*4]
    echo rx_result=$rx_result
        tx_result=$[(tx_after-tx_before)*4]
    echo tx_result=$tx_result
        echo "$time Now_In_Speed: "$rx_result"bps Now_Out_Speed: "$tx_result"bps"
        sleep 2
done











评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值