Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1821531
  • 博文数量: 116
  • 博客积分: 9934
  • 博客等级: 上将
  • 技术积分: 1881
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-22 09:16
文章分类

全部博文(116)

文章存档

2007年(43)

2006年(73)

我的朋友

分类:

2006-04-22 09:16:00

shell技术要点

 

 

  

1.        学习提示

学习shell主要在于用,光看书没用

单引号内所有元字符都失去特殊含义(包括\)双引号内除了变量域$和命令域`以外的元字符都失去特殊含义,所以一般使用双引号引用

花括号{}被用来区分变量名和周围的文本:echo ${file} and $file1 寻找变量file,file1

命令替代的格式:反引号来环绕一个命令象` cmd `,它和$(command) 是等价的:ls -l `find . -type f`

sed -n "3"p file 取第3

sed -n "1,3"p file 取第13

sed -n "1,$"p file 取第1到最后一行

sed -n "1,$num"p file 取第1num

sed -n "\$p" file 取最后1

sed -e '1!G;h;$!d' file倒过来显示

附加/替换:

sed "/xmdh/a\daoyou" file 把含有xmdh的行的结尾附加daoyou(有换行)

sed 's/$/ daoyou/' file把每行的结尾附加daoyou(在同一行)

sed '/test/s/$/ daoyou/' file把包含test行的结尾附加daoyou(在同一行)

sed '10s/$/ daoyou/' file把第10行的结尾附加daoyou(在同一行)

 

sed "s/xmdh/daoyou/g" filexmdh替换成daoyou

sed  "s/xmdh/daoyou/;G"  filexmdh替换成daoyou并增加一个换行

cat userlog |sed -n '/xmdh/ w test.txt'查看含有xmdh并写入test.txt

vi file 后:/\/

awk '/dhshunde/{print NR,$0}' userloggrep -n dhshunde userlog 显示含有dhshunde的行号及内容

cat userlog |sed -n ‘/erptest/=’ 显示含有erptest的行号

cat userlog |sed -n '/xmdh/p'|sed -n '$p' 显示包含xmdh的最后一行

中使用变量

/bin/cat /etc/ppp/chap-secrets|grep $5|awk '{print logouttime"\t","username:"$1"\t","logout""\t","data:"datasize}' logouttime="`/bin/date`" datasize="$size" >>$pptplogdirectory/userlog(注:size前面已经有定义)

的用法

注:f i n d命令将所有匹配到的文件一起传递给e x e c执行,而x a rg s命令每次只获取一部分文件而不是全部,所以exec有长度限制,文件不能太多,否则会产生溢出错误,而xargs则没有

find . -mtime -1 –print 跟现在小于1天修改的文件

find . -perm 755 –print 显示具有755属性的文件

find . -size +1000000c –print 查找大于1M的文件

find . -type f -exec ls -l {} \; 查找文件并列表显示(注:{}\之间有空格,最后有;

find . -type f -exec rm  {} \;查找文件并删除

find . -type f -print |xargs ls –l查看文件并列表显示

find /  \( -perm -4000 -o -perm -2000 \) -type f –print 查找SUIDSGID文件

echo "hello I am jiangdaoyou"|tee /dev/pts/2 (tty可以查看自已的终端号),等同于:write root pts/2然后输入:hello I am jiangdaoyou然后Ctrl+D结束

BEGINEND

即在文件头增加列名:

cat userlog |awk 'BEGIN{print "Time username\n-----------------"};{print $4,$7}'

Time     username

------------------------------

15:19:28 username:xmdh

15:20:00 username:xmdh

将在上面的基础上增加结尾说明“end of report!!!!

cat userlog |awk 'BEGIN{print "Time     username\n-----------------"}{print $4,$7}END{print "end of report!!!!"}'

返回

 

转化字符

echo "200604211031"|cut -c9-12 得到1031

cat test.ok |tr 'arp' 'rpm' arp转为rpm

vmstat 1 4|awk '{print $4}'|grep -o '[0-9]*'|sed 's/,//g' |awk '{total=total+$1;if(NR%4= =0) {print total/4}}'或如下方法:

vmstat 1 4|awk 'NR>2{sum+=$4}END{print sum/4}'

ls |for file in *;do echo "rpm -ivh" $file;done

ls |for file in $(ls *.rpm);do echo "rpm -ivh" $file;done

阅读(3609) | 评论(12) | 转发(0) |
0

上一篇:没有了

下一篇:linux下pptp服务器配置

给主人留下些什么吧!~~

jatnet2008-03-10 18:57:38

非常有心,谢谢发布者!!

jatnet2008-03-10 18:57:38

非常有心,谢谢发布者!!