一,diff和patch命令
diff通常比较文件的内容,patch常用来打补丁
1,比较内容
[root@100 mnt]# vim file1 [root@100 mnt]# vim file2 [root@100 mnt]# cat file1 123 haha [root@100 mnt]# cat file2 123 nihao [root@100 mnt]# diff file1 file2 >>>>>注意是以第二个文件为基准的 2c2 >>>>第一个文件的第2行 修改变成 第二个文件的第2行 < haha < 表示第一个文件中的内容 --- > nihao > 表示第二个文件中的内容a 表示添加 ----add
c 表示更改 ----change
d 表示删除 ----delete
< 表示第一个文件中的内容, > 表示第二个文件中的内容, --- 分割线
[root@100 mnt]# vim file1 [root@100 mnt]# cat file1 123 [root@100 mnt]# cat file2 123 nihao [root@100 mnt]# diff file1 file2 1a2 >>>>第一个文件的第1行 添加 第二个文件的第2行 > nihao [root@100 mnt]# diff file2 file1 2d1 >>>>第一个文件的第2行 删除 变成 第二个文件的第1行 < nihao
[root@100 mnt]# diff /etc/passwd /etc/shadow 1,39c1,39 >>>>>!!!注意,这里1,39是1-39的意思,不是1和39的意思,是39行 < root:x:0:0:root:/root:/bin/bash < bin:x:1:1:bin:/bin:/sbin/nologin < daemon:x:2:2:daemon:/sbin:/sbin/nologin < adm:x:3:4:adm:/var/adm:/sbin/nologin . . .
2,生成补丁
[root@100 mnt]# diff -u file2 file1 >>>>>-u 产生补丁 注意:这里一定是和后者做比较 --- file2 2018-06-10 10:38:12.882174676 -0400 +++ file1 2018-06-10 10:43:30.352174676 -0400 @@ -1,2 +1 @@ 123 -nihao [root@100 mnt]# diff -u file2 file1 > file2.path >>>>把补丁导入.path文件,注意后缀,注意是以后一个文件为基准,补丁是对前一个打补丁 [root@100 mnt]# ls file1 file2 file2.path
3,打补丁
[root@100 mnt]# ls file1 file2 file2.path [root@100 mnt]# yum install patch -y >>>>>安装patch打补丁软件 [root@100 mnt]# cat file1 123 [root@100 mnt]# cat file2 123 nihao [root@100 mnt]# cat file2.path >>>>>查看补丁 --- file2 2018-06-10 10:38:12.882174676 -0400 +++ file1 2018-06-10 10:43:30.352174676 -0400 @@ -1,2 +1 @@ 123 -nihao [root@100 mnt]# patch file2 file2.path >>>>>>给file2打补丁,此时file2和file1就一样了 patching file file2 [root@100 mnt]# cat file2 123
这样打补丁原文件就没有了
[root@100 mnt]# ls file1 file2 file2.path
4,保留原文件并打补丁
[root@100 mnt]# vim file3 [root@100 mnt]# cat file3 123 haha [root@100 mnt]# cat file1 123 [root@100 mnt]# diff -u file1 file3 > file1.path >>>>>生成file1的补丁 [root@100 mnt]# patch -b file1 file1.path >>>>>>-b,打补丁的同时,备份原文件 patching file file1 [root@100 mnt]# cat file1 >>>>>>此时file1和file3的内容一样了 123 haha [root@100 mnt]# ls >>>>保留了原文件,file1.orig就是原文件 file1 file1.orig file1.path file2 file2.path file3
5,diff比较目录
[root@100 mnt]# diff -r /etc/ /mnt/ >>>>>比较目录需要加上-r,比较的结果是目录中文件的不同,而不是文件内容的不同 Only in /etc/: abrt Only in /etc/: adjtime . . .
二,cut命令
通常用来截取
[root@100 mnt]# cat passwd root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin games:x:12:100:games:/usr/games:/sbin/nologin ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
1,截取段
[root@100 mnt]# cut -d : -f 1 passwd >>>> -d 指定分隔符为: -f 指定要截取的列 第1列 root bin daemon adm lp sync shutdown halt mail operator games ftp [root@100 mnt]# cut -d : -f 1,3 passwd >>>>-d 指定分隔符 -f 指定要截取的列 第1和3列 root:0 bin:1 daemon:2 adm:3 lp:4 sync:5 shutdown:6 halt:7 mail:8 operator:11 games:12 ftp:14 [root@100 mnt]# cut -d : -f 1-3 passwd >>>>>-d 指定分隔符 -f 指定要截取的列 第1到3列 root:x:0 bin:x:1 daemon:x:2 adm:x:3 lp:x:4 sync:x:5 shutdown:x:6 halt:x:7 mail:x:8 operator:x:11 games:x:12 ftp:x:14 [root@100 mnt]# cut -d : -f 3- passwd >>>>>>-d 指定分隔符 -f 指定要截取的列 第3列之后 0:0:root:/root:/bin/bash 1:1:bin:/bin:/sbin/nologin 2:2:daemon:/sbin:/sbin/nologin 3:4:adm:/var/adm:/sbin/nologin 4:7:lp:/var/spool/lpd:/sbin/nologin 5:0:sync:/sbin:/bin/sync 6:0:shutdown:/sbin:/sbin/shutdown 7:0:halt:/sbin:/sbin/halt 8:12:mail:/var/spool/mail:/sbin/nologin 11:0:operator:/root:/sbin/nologin 12:100:games:/usr/games:/sbin/nologin 14:50:FTP User:/var/ftp:/sbin/nologin
2,截取字符
[root@100 mnt]# cut -c 1-3 passwd >>>>>>-c 指定截取的字符的位置,第1-3列 roo bin dae adm lp: syn shu hal mai ope gam ftp [root@100 mnt]# cut -c 1,3 passwd >>>>>>>-c 指定截取的字符的位置,第1和3列 ro bn de am l: sn su hl mi oe gm fp
三,shell编程练习1
1,找出ip地址
方法一
[root@100 mnt]# cat ip_show.sh ################################# # Author: Minz # # Version: # # Mail: [email protected] # # Date: 2018-06-10 # # Description # # # ################################# #!/bin/bash ifconfig eth0 | grep "inet\>" | cut -d " " -f 10 \>是词尾定位符 grep "inet"会把带inet的行全打印出来,这样会出来inet和inet6,grep "inet\>"只打印inet的那一行 输出通过管道符,指定以空格为分隔符的第十段,截取出来
测试
[root@100 mnt]# sh ip_show.sh 172.25.254.100 成功!!!!!! [root@100 mnt]# sh -x ip_show.sh >>>>-x显示过程 + cut -d ' ' -f 10 + ifconfig eth0 + grep 'inet\>' 172.25.254.100
方法二
[root@100 mnt]# cat ip_show.sh ################################# # Author: Minz # # Version: # # Mail: [email protected] # # Date: 2018-06-10 # # Description # # # ################################# #!/bin/bash ifconfig eth0 | awk -F " " '/inet\>/{print $2}' 以空格为分隔符,打印inet后面的第二段 \>是词尾定位符 /用来屏蔽一个元字符的特殊含义。因为有时在shell中一些元字符有特殊含义。\可以使其失去应有意义
测试
[root@100 mnt]# sh ip_show.sh 172.25.254.100 成功!!!!! [root@100 mnt]# sh -x ip_show.sh + ifconfig eth0 + awk -F ' ' '/inet\>/{print $2}' 172.25.254.100
四,shell编程中的条件表示&&,||
&& 用来执行条件成立后执行的命令
|| 用来执行条件不成立后执行的命令
五,shell编程练习2
判断一个ip是否连通
[root@100 Desktop]# vim check_ip.sh [root@100 Desktop]# cat check_ip.sh ################################# # Author: Minz # # Version: # # Mail: [email protected] # # Date: 2018-06-13 # # Description # # # ################################# #!/bin/bash ping -c1 -w1 $1 &> /dev/null && echo $1 is up || echo $1 is down -c1表示只ping一次 -w1表示等待时间 $1表示第一个变量 &> /dev/null 表示把所有信息(正确的和错误的)都扔进垃圾箱 如果条件成立,ping通 打印 ip 如果条件不成立,ping不通 打印 down
测试
[root@100 Desktop]# sh check_ip.sh 172.25.254.156 检测成功! 172.25.254.156 is up [root@100 Desktop]# sh check_ip.sh 172.25.254.333 检测成功! 172.25.254.333 is down [root@100 Desktop]# sh -x check_ip.sh 172.25.254.156 -x表示执行过程 + ping -c1 -w1 172.25.254.156 + echo 172.25.254.156 is up 172.25.254.156 is up
六,sort命令和uniq命令
sort命令通常用来排序
uniq命令对重复字符做相应的处理
1,
[root@100 Desktop]# vim numbers [root@100 Desktop]# cat numbers 1 3 5 34 10 2 1 5 1 1 [root@100 Desktop]# sort numbers >>>>默认是正向排序,而且是对每行的第一个单个数字进行排序 1 1 1 1 10 2 3 34 5 5 [root@100 Desktop]# sort -n numbers >>>>-n 表示纯数字排序 1 1 1 1 2 3 5 5 10 34 [root@100 Desktop]# sort -r numbers >>>>> -r 表示反向排序,也是对每行的第一个单个数字排序 5 5 34 3 2 10 1 1 1 1 [root@100 Desktop]# sort -rn numbers >>>>>反向纯数字排序 34 10 5 5 3 2 1 1 1 1 [root@100 Desktop]# sort -rn numbers -o NUM >>>>-o 指定输出文件 [root@100 Desktop]# cat NUM 34 10 5 5 3 2 1 1 1 1 [root@100 Desktop]# sort -u numbers >>>>>-u表示unique独一无二的,去掉重复数字 1 10 2 3 34 5 [root@100 Desktop]# sort -urn numbers >>>>>去掉重复数字纯数字反向排序 34 10 5 3 2 1
vim可视化列插入:ctrl+v > 选择列 > 输入大写的I > 输入字符 > esc
[root@100 Desktop]# vim numbers [root@100 Desktop]# cat numbers 1:1 2:3 5:5 2:34 22:10 1:2 3:1 2:5 10:1 3:1 [root@100 Desktop]# sort -t : -k 2 numbers >>> -t 指定分隔符为: -k 指定列 第2列 !!!!!注意,对列进行处理,行内容不会改变 10:1 1:1 3:1 3:1 22:10 1:2 2:3 2:34 2:5 5:5 [root@100 Desktop]# sort -t : -k 2 numbers -n >>>> 以:为分隔符,对第2列纯数字正向排序 10:1 1:1 3:1 3:1 1:2 2:3 2:5 5:5 22:10 2:34 [root@100 Desktop]# sort -t : -k 1 numbers -rnu >>>> 以:为分隔符 ,对第一列去掉重复数字之后反序 22:10 10:1 5:5 3:1 2:3 1:1
[root@100 Desktop]# vim numbers [root@100 Desktop]# cat numbers 1 3 5 34 10 2 1 5 1 1 [root@100 Desktop]# sort -n numbers | uniq -c >>>>进行纯数字排序之后 重复数字只显示一次,并且在左边显示重复次数 4 1 1 2 1 3 2 5 1 10 1 34 [root@100 Desktop]# sort -n numbers | uniq -d >>>>进行纯数字排序之后显示重复的行 1 5 [root@100 Desktop]# sort -n numbers | uniq -u >>>>进行纯数字排序之后显示不重复的行 2 3 10 34
七,shell编程练习
显示/mnt目录最大的文件的文件名
[root@100 Desktop]# ls -l /mnt/ total 8 -rw-r--r--. 1 root root 333 Jun 10 12:07 ip_show.sh drwxr-xr-x. 2 root root 6 Jun 12 04:37 multiuser drwxr-xr-x. 2 root root 6 Jun 11 23:45 nfsshare -rw-r--r--. 1 root root 475 Jun 10 11:26 passwd [root@100 Desktop]# ls -Sl /mnt/ | grep -v total | awk -F " " 'NR==1{print $9}' -S就是按照文件大小排序显示 -v对是反向抓取,把抓取到的内容去掉然后再显示 -F指定分隔方式 NR==1表示第一行 $9表示第9列 passwd