2 unit 6

来源:互联网 发布:自己的淘宝账号怎么看 编辑:程序博客网 时间:2024/06/06 23:50
###unit6.shell脚本命令###


###1.diff
diff file file1##


比较两个文件的不同


diff -c ##显示周围的行
diff


-u ##按照格式统一输出生成的补丁
diff -r


##比较两个目录文件的不同


patch
patch


file file.path ##打补丁
patch -b ##备份原文





###2.grep
grep 关键字符 文件|目录 ##在文件或目录中查


找含有关键字的行


grep -i ##忽略大小写
grep


-n ##显示关键字所在行
grep -c


##显示过滤结果的个数
grep -v ##反向过滤
grep -E "关键字1|关键字2"##过滤多个关键字
grep -r 关键字 目录##在目录中查找含有关键字的文件


注意


:^关键字 ##以关键字开头
      关键字$


##以关键字结尾


###3.cut
cut ##


截取字符


cut -d 分隔符 ##指定分隔符
cut -f 1,7


##显示指定的列
cyt -c 1-4 ##显示指定


的字符


只显示ifconfig eth0中的ip地址
[root@localhost ~]# ifconfig 


eth0 | grep inet | grep inet6 -v | cut -d " " -f 10
[root@localhost 


~]# ifconfig eth0 | grep inet | grep inet6 -v | awk -F " " '{print 


$2}'


###4.sort
sort ##排序


sort -n


##纯数字排序
sort -u ##


去冗余(去除重复的)
sort |uniq -c ##去除冗余并统计冗


余次数
sort -t ##指定分隔符
sort -k


##指定列


###5.uniq
sort file | uniq -c ##


去掉冗余并统计冗余的次数
sort file | uniq -d ##显示冗余



sort file | uniq -u ##显示唯一行


###6.sed
sed 's/原字符/


替换字符/g' file
sed -e '策略1' -e '策略2' file
sed -i file


##把转换后的内容输入到指定文件
sed '3,5s/原字符/替换字符/g' 


file ##3—5行替换
sed xd ##屏蔽指定行
sed xp


##复制指定行
sed -n xp ##


只显示指定行
sed -f 策略文件 file ##表示指定策略文件


###脚本
userfile
westos1
westos2
westos3


passwdfile
redhat1
redhat2
redhat3


user_create.sh
#!/bin/bash
MAX=$( wc -l $1 | cut -d " " -f 1 )


for NUM 


in $( seq 1 $MAX )
do
        USERNAME=$( sed -n ${NUM}p $1)
        


PASSWD=$( sed -n ${NUM}p $2)
        useradd $USERNAME
        echo 


$PASSWD | passwd --stdin $USERNAME
done


测试
[root@desktop18 mnt]# sh 


user_create.sh userfile passwdfile 
Changing password for user 


westos1.
passwd: all authentication tokens updated successfully.
Changing password for user westos2.
passwd: all authentication 


tokens updated successfully.
Changing password for user westos3.
passwd: all authentication tokens updated successfully.




###tr
[root@desktop2 mnt]# cat file 
a
a
a
a
a
[root@desktop2 mnt]# tr 'a-z' 


'A-Z' < file
A
A
A
A
A


[root@desktop2 mnt]# tr 'a' 'Z' < file
Z
Z
Z
Z
Z





0 0
原创粉丝点击