1、diff命令的使用方法
用法:
diff [options] files|directorys
输出信息: [num1,um2] [a|c|d] [num3,num4]
num1,num2 第一个文件中的行 a 添加 c 更改 d 删除 < 第一个文件中的内容 > 第二个文件中的内容 num3,num4 第二个文件中的行
[root@server1 ~]# cd /mnt/
[root@server1 mnt]# vim westos
hello westos
[root@server1 mnt]# vim westos.news
hello westos
123
[root@server1 mnt]# diff westos westos.news
1a2 其中1表示第一个文件的行,2表示第二个文件的行,a表示添加
> 123 >表示第二个文件中的内容
常用参数:
-b ##忽略空格 -B ##忽略空行 -i ##忽略大小写 -c ##显示文件所有内容并标示不同 -r ##对比目录 -u ##合并输出
[root@server1 mnt]# vim westos
hello westos
[root@server1 mnt]# vim westos.news
hello westos 添加一个空格
[root@server1 mnt]# diff westos westos.news
1c1
< hello westos
---
> hello westos
[root@server1 mnt]# diff -b westos westos.news -b 表示忽略,忽略空格后两个文件对比内容一样
[root@server1 mnt]# vim westos.news
hello westos
加一行空行
[root@server1 mnt]# diff westos westos.news
1a2
> 表示第二个文件的第二行有空格
[root@server1 mnt]# diff -B westos westos.news 表示忽略空行
[root@server1 mnt]# vim westos.news
hello Westos 修改为大写
[root@server1 mnt]# diff westos westos.news
1c1
< hello westos
---
> hello Westos
[root@server1 mnt]# diff -i westos westos.news 忽略大小写
[root@server1 mnt]# vim westos.news
123
hello Westos
456
[root@server1 mnt]# vim westos
123
hell Westos
456
[root@server1 mnt]# diff -c westos westos.news -c表示显示文件所有内容,并标识不同
*** westos 2022-09-02 11:16:32.631000000 +0800
--- westos.news 2022-09-02 11:15:09.527000000 +0800
***************
*** 1,3 ****
123
! hell Westos
456
--- 1,3 ----
123
! hello Westos
456
[root@server1 mnt]# mkdir westosdir westosdir1
[root@server1 mnt]# touch westosdir1/file
[root@server1 mnt]# diff -r westosdir westosdir1/ -r表示对比两个目录
Only in westosdir1/: file
[root@server1 mnt]# diff -u westos westos.news > westos.path -u 表示合并输出,用于打补丁
--- westos 2022-09-02 11:16:32.631000000 +0800
+++ westos.news 2022-09-02 11:15:09.527000000 +0800
@@ -1,3 +1,3 @@
123
-hell Westos
+hello Westos
456
[root@server1 mnt]# yum install patch -y 打补丁需要安装补丁管理器
[root@server1 mnt]# patch westos westos.path 在westos上打补丁
patching file westos
[root@server1 mnt]# cat westos 可以发现和westos.news内容一致
123
hello Westos
456
注意:打完补丁后原文件就没了,需要备份怎么办了?
[root@server1 mnt]# diff -u westos westos.news > westos.path 修改过后的文件需要重新打补丁
[root@server1 mnt]# patch -b westos westos.news -b 表示打补丁后保存原文件
[root@server1 mnt]# patch westos westos.path
patching file westos
[root@server1 mnt]# ls
westos westosdir westosdir1 westos.news westos.orig westos.path westos.orig 为备份的原文件
[root@server1 mnt]# cat westos 打补丁的文件也打上去了
123
hello Westos
456
2. sort
-n ##纯数字排序 -r 倒叙 -u 去掉重复 -o 输出到指定文件 -t 指定分割符 -k 指定排序的列
[root@server1 mnt]# cat westos
5
9
2
4
5
1
56
9
1
0
5
4
3
8
[root@server1 mnt]# sort westos 排序,默认是以第一行排序
0
0
0
0
1
1
1
1
1
13
13
[root@server1 mnt]# sort -n westos 纯数字排序
0
0
1
1
1
1
2
3
4
4
[root@server1 mnt]# sort -rn westos 倒叙排序
56
56
45
13
9
9
9
8
5
5
4
4
3
[root@server1 mnt]# sort -n westos -o testfile -o表示将排序结果输出到指定文件
[root@server1 mnt]# ls
testfile westos
[root@server1 mnt]# sort -un westos -u表示去掉重复项
0
1
2
3
4
5
8
9
13
45
56
[root@server1 mnt]# vim westos
a:1
a:56
a:9
a:1
a:0
a:5
a:4
a:3
3:8
4:0
5:13
6:45
3:9
a:2
a:4
a:5
a:1
a:56
6:9
1:1
[root@server1 mnt]# sort -n -t : -k 2 westos -t表示指定分隔符 -k 2 表示指定第二列排序
4:0
a:0
1:1
a:1
a:1
a:1
a:2
a:3
a:4
a:4
a:5
a:5
3:8
3:9
6:9
a:9
5:13
3. uniq (结合sort排序命令使用)
-c 合并重复并统计重复个数 -d 显示重复的行 -u 显示唯一的行
[root@server1 mnt]# vim westos
1
56
9
1
0
5
4
3
8
0
13
45
9
2
4
5
1
56
9
1
[root@server1 mnt]# sort -n westos | uniq -c uniq -c 表示合并重复的项,并显示重复个数
2 0
4 1
1 2
1 3
2 4
2 5
1 8
3 9
1 13
1 45
2 56
[root@server1 mnt]# sort -n westos | uniq -d 显示重复的行
0
1
4
5
9
56
[root@server1 mnt]# sort -n westos | uniq -u 显示唯一的行
2
3
8
13
45
4. cut
-d : 指定 : 为分割符 -f 指定显示的列 ,5第五列 ,3,5 3和5列,3-5 3到5列, 5- 第五列以后 ,-5 到第五列 -c 指定截取的字符(数字用法同-f)
[root@server1 mnt]# cp /etc/passwd .
[root@server1 mnt]# cut -d : -f 1 passwd -d表示指定分隔符, -f 1 显示第一列
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
[root@server1 mnt]# cut -d : -f 1,2 passwd -f 1,2显示第一列和第二列
root:x
bin:x
daemon:x
adm:x
lp:x
sync:x
shutdown:x
halt:x
mail:x
operator:x
games:x
[root@server1 mnt]# cut -d : -f 1-4 passwd 显示1到4列
root:x:0:0
bin:x:1:1
daemon:x:2:2
adm:x:3:4
lp:x:4:7
sync:x:5:0
shutdown:x:6:0
halt:x:7:0
mail:x:8:12
operator:x:11:0
games:x:12:100
[root@server1 mnt]# cut -d : -f 4- passwd 显示第4列到最后一列
0:root:/root:/bin/bash
1:bin:/bin:/sbin/nologin
2:daemon:/sbin:/sbin/nologin
4:adm:/var/adm:/sbin/nologin
7:lp:/var/spool/lpd:/sbin/nologin
0:sync:/sbin:/bin/sync
0:shutdown:/sbin:/sbin/shutdown
0:halt:/sbin:/sbin/halt
12:mail:/var/spool/mail:/sbin/nologin
0:operator:/root:/sbin/nologin
100:games:/usr/games:/sbin/nologin
[root@server1 mnt]# cut -d : -f -4 passwd 显示第一列到第4列
root:x:0:0
bin:x:1:1
daemon:x:2:2
adm:x:3:4
lp:x:4:7
sync:x:5:0
shutdown:x:6:0
halt:x:7:0
mail:x:8:12
operator:x:11:0
games:x:12:100
[root@server1 mnt]# cut -c 2 passwd 截取每行第2个字符
o
i
a
d
p
y
h
a
a
p
a
[root@server1 mnt]# cut -c 1-2 passwd 截取每行第1到第4个字符
ro
bi
da
ad
lp
sy
sh
ha
ma
op
ga
[root@server1 mnt]# cut -c 4- passwd 截取每行第4个字符到最后的字符
t:x:0:0:root:/root:/bin/bash
:x:1:1:bin:/bin:/sbin/nologin
mon:x:2:2:daemon:/sbin:/sbin/nologin
:x:3:4:adm:/var/adm:/sbin/nologin
x:4:7:lp:/var/spool/lpd:/sbin/nologin
c:x:5:0:sync:/sbin:/bin/sync
tdown:x:6:0:shutdown:/sbin:/sbin/shutdown
t:x:7:0:halt:/sbin:/sbin/halt
l:x:8:12:mail:/var/spool/mail:/sbin/nologin
rator:x:11:0:operator:/root:/sbin/nologin
es:x:12:100:games:/usr/games:/sbin/nologin
[root@server1 mnt]# cut -c 1,4 passwd 截取每行第1和第4的字符
rt
b:
dm
a:
lx
sc
st
ht
ml
or
ge
5. && ||
[root@server1 mnt]# ping -c1 -w1 172.25.254.111 &>/dev/null && echo yes || echo no 表示能ping通显示yes,不能ping通表示no
no
[westos@server1 ~]$ find /etc/ -name passwd &> /dev/null && echo yes || echo no 命令有报错显示no
no
[westos@server1 ~]$ find /etc/ -name passwd &> /dev/null && (echo yes;echo 1) || (echo no;echo 2) 可以执行多条,用括号阔起来表示一个整体
no
2
在脚本里书写方法:
[root@server1 ~]# vim test.sh
ping -c1 -w1 172.25.254.111 &> /dev/null && { 符合条件取123
echo 1
echo 2
echo 3
}||{ 不符合条件取456
echo 4
echo 5
echo 6
}
[root@server1 ~]# sh test.sh 运行
4
5
6
6. tr
tr ‘a-z’ ‘A-Z’ 小写转大写 tr ‘A-Z’ ‘a-z’ 大写转小写
[root@server1 mnt]# vim westos
hello westos
[root@server1 mnt]# tr 'a-z' 'A-Z' < westos
HELLO WESTOS
[root@server1 mnt]# tr 'l' 'a' < westos 可以转换字符,只能转换一个,不能批量转换
heaao westos
7、test
7.1 test数字对比
-eq 等于 -ne 不等于 -le 小于等于 -lt 小于 -ge 大于等于 -gt 大于
[root@server1 ~]# a=1
[root@server1 ~]# b=1
[root@server1 ~]# test "$a" = "$b" && echo yes || echo no
yes
[root@server1 ~]# [ "$a" = "$b" ] && echo yes || echo no test也可以写成中括号
yes
[root@server1 ~]# [ "$a" -eq "$b" ] && echo yes || echo no -eq也表示相等
yes
[root@server1 ~]# [ "$a" -ne "$b" ] && echo yes || echo no -ne表示不等
no
[root@server1 ~]# [ "$a" -lt "$b" ] && echo yes || echo no -lt表示小于
no
[root@server1 ~]# [ "$a" -le "$b" ] && echo yes || echo no -le表示小于等于
yes
[root@server1 ~]# [ "$a" -gt "$b" ] && echo yes || echo no -gt表示大于
no
[root@server1 ~]# [ "$a" -ge "$b" ] && echo yes || echo no -ge表示大于等于
yes
[root@server1 ~]# [ ! "$a" -ge "$b" ] && echo yes || echo no !表示条件如果不成立为真,反选
no
7.2 test条件对比
[root@server1 ~]# c=5
[root@server1 ~]# [ "$c" -gt "0" -a "$c" -lt "10" ] && echo yes || echo no 判定5是不是在10以内,-a 并且
yes
[root@server1 ~]# [ "$c" -le "0" -o "$c" -ge "10" ] && echo yes || echo no 判定5是不是在10以外 -o 或者
no
7.3 对空的判定
-n nozero 判定内容不为空 -z zero 判定内容为空
[root@server1 ~]# [ -z "$d" ] && echo yes || echo no -z 判定内容为空
yes
[root@server1 ~]# [ -n "$d" ] && echo yes || echo no -n 判定内容不为空
no
[root@server1 ~]# d=5
[root@server1 ~]# [ -n "$d" ] && echo yes || echo no d=5不为空,所以输出结果为yes
yes
7.4 test对于文件的判定
-ef 文件节点号是否一致(硬链) -nt 文件1是不是比文件2新 -ot 文件1是不是比文件2老 -d 目录 -S 套结字 -L 软连接 -e 存在 -f 普通文件 -b 快设备 -c 字符设备
[root@server1 mnt]# [ "/mnt/westos1" -nt "/mnt/westos" ] && echo yes || echo no 判定westos1是否比westos新
yes
[root@server1 mnt]# [ "/mnt/westos1" -ot "/mnt/westos" ] && echo yes || echo no 判定westos1是否比westos老
no
[root@server1 mnt]# ln /mnt/westos1 /mnt/westos2 软连接
[root@server1 mnt]# ls -i /mnt/
33575112 westos 33575114 westos1 33575114 westos2
[root@server1 mnt]# [ "/mnt/westos1" -ef "/mnt/westos2" ] && echo yes || echo no 判定westos1和westos2节点号(id)是否一致
yes
[root@server1 mnt]# [ "/mnt/westos1" -ef "/mnt/westos" ] && echo yes || echo no
no
[root@server1 mnt]# [ -e "/mnt/westos" ] && echo yes || echo no -e判定文件是否存在
yes
[root@server1 mnt]# [ -d "/mnt/westos" ] && echo yes || echo no -d判定是不是目录
no
[root@server1 mnt]# [ -f "/mnt/westos" ] && echo yes || echo no -f判定是不是文件
yes
8、练习
test :
ifconfig 网卡,可以显示此网卡的信息 显示信息中包含此网卡使用的ip 地址
[root@server1 mnt]# ifconfig eth0 | head -n 2 | tail -n 1 | cut -d " " -f 10
172.25.50.1
[root@server1 mnt]# ifconfig eth0 | awk '/inet\>/{print $2}'
172.25.50.1
2、如何判定用户存不存在
[root@server1 mnt]# vim test.sh
#!/bin/bash
id $* &> /dev/null && {
echo $* is exist
}||{
echo $* is not exist
}
[root@server1 mnt]# sh test.sh hello
hello is not exist
[root@server1 mnt]# sh test.sh westos
westos is exist