您的位置:首页 > 运维架构 > Shell

shell脚本实例解析

2011-08-10 16:36 260 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: