一、diff在比较文件过程中结果读取方式
- 概念
[num1 , num2][a/c/d][num3 , num4]
nmu1,num2表示在第一个文件中的行数
a表示添加 —add
c表示更改 —change
d表示删除 —delete
<表示第一个文件中的内容,>表示第二个文件中的内容,
—表示分隔线
num3 , num4表示在第二个文件中的行数
2,4c2,4表示改变第一个文件中的第二行和第二行和第四行才能匹配第二个文件中的第二行和第四行 - 实验
1.增加
[root@shell_example mnt]# vim test
[root@shell_example mnt]# vim test1
[root@shell_example mnt]# cat test
hello westos
[root@shell_example mnt]# cat test1
hello westos
123
[root@shell_example mnt]# diff test test1
1a2
> 123
输出的结果表示:当给第一个文件的第一行添加123后才能匹配第二个文件的第二行
2.删除
将比较的文件换个位置后结果也会不同
[root@shell_example mnt]# diff test1 test
2d1
< 123
3.更改
[root@shell_example mnt]# vim test
[root@shell_example mnt]# cat test
hello westos
......
[root@shell_example mnt]# cat test1
hello westos
123
[root@shell_example mnt]# diff test test1
2c2
< ......
---
> 123
输出的结果表示:当将第一个文件的第二行更改成第二个文件中的第二行时才能匹配第二个文件中的第二行
4.布丁的使用
patch:用于文件不同文件打布丁
patch[options]file.old file.path
<1>比较test和test1中的内容(布丁中的内容)
[root@shell_example mnt]# diff -u test test1
输出的结果表示:文件内容的不同
<2>将test1中的内容放到补丁
[root@shell_example mnt]# diff -u test test1 > test.path
<3>安装patch软件,然后将test中的内容利用patch命令进行修改,查看test和test1的内容是相等的
[root@shell_example mnt]# yum install patch -y
[root@shell_example mnt]# patch test test.path
patching file test
[root@shell_example mnt]# cat test
hello westos
123
[root@shell_example mnt]# cat test1
hello westos
123
二.diff在比较目录过程中结果的读取
Only in directory/:filename
directory表示在哪个目录中
filename表示在哪个目录
1.新建目录westos和westos1,diff目录westos和目录westos1,没有报错
[root@shell_example mnt]# mkdir westos
[root@shell_example mnt]# mkdir westos1
[root@shell_example mnt]# diff -r westos westos1
2.在westos目录下新建一个file,再次diff时,会报错
[root@shell_example mnt]# touch westos1/file
[root@shell_example mnt]# diff -r westos westos1
Only in westos1: file
3.diff中常用的参数
-b或–ignore-space-change不检查空格字符的不同
-B或–ignore-blank-lines不检查空白处
-c 显示全部内容,并标出不同之处
-i或–Ignore-case不检查大小写的不同
-p:若比较的文件为c语言的程序码文件时,显示差异所在的函数名称
-q或–brief:仅显示有无差异,不显示详细的信息
—r或–recursive:比较子目录中的文件
<1>diff -b
[root@shell_example mnt]# vim test
[root@shell_example mnt]# cat test
hello westos
[root@shell_example mnt]# vim test1
[root@shell_example mnt]# cat test1
hello westos
[root@shell_example mnt]# diff test test1
1c1
< hello westos
---
> hello westos
[root@shell_example mnt]# diff -b test test1
注意:两个文件内中的内容中有空格字符不同,所以使用-b参数时不会报错
<2>diff -B
[root@shell_example mnt]# vim test
[root@shell_example mnt]# cat test
hello westos
[root@shell_example mnt]# vim test1
[root@shell_example mnt]# cat test1
hello westos
[root@shell_example mnt]# diff test test1
2a3
>
[root@shell_example mnt]# diff -B test test1
注意:两个文件内中的内容的空白处不同,所以使用-B参数时不会报错
<3>diff-c1/c2/c3/c的用法
[root@shell_example mnt]# vim test
[root@shell_example mnt]# cat test
hello westos
linux
123
java
[root@shell_example mnt]# vim test1
[root@shell_example mnt]# cat test1
hello westos
linux
123
1
java
[root@shell_example mnt]# diff -c1 test test1 ##显示报错的前一条和报错的后一条
[root@shell_example mnt]# diff -c2 test test1 ## 显示报错的前两条和报错的后一条
[root@shell_example mnt]# diff -c3 test test1 ##显示报错的前三条和报错的后一条
[root@shell_example mnt]# diff -c test test1 ##显示全部内容以及报错的信息
<4>diff -p
[root@shell_example mnt]# vim test.c
[root@shell_example mnt]# vim test1.c
[root@shell_example mnt]# diff -p test.c test1.c
<5>diff -q
[root@shell_example mnt]# yum install gcc -y
[root@shell_example mnt]# ls
test1.c test.c
[root@shell_example mnt]# gcc test.c -o test
[root@shell_example mnt]# gcc test1.c -o test1
[root@shell_example mnt]# ls
test test1 test1.c test.c
[root@shell_example mnt]# diff -p test test1
[root@shell_example mnt]# diff -p test.c test1.c
[root@shell_example mnt]# diff -q test.c test1.c
三、cut命令
cut命令多用于字符截取
cut -d 指定分隔符
cut -f 1,7|1-7 指定截取的列
cut -c 1,4|1-4 指定截取的字符位置
1.把/etc/passwd文件复制到/mnt/下
[root@shell_example mnt]# ls
[root@shell_example mnt]# cp /etc/passwd .
[root@shell_example mnt]# ls
passwd
[root@shell_example mnt]# vim passwd ##可删除一部分,能看到效果即可
2.指定截取passwd文件中的第一列
[root@shell_example mnt]# cut -d : -f 1 passwd
3.指定截取passwd文件中的第一列和第七列
[root@shell_example mnt]# cut -d : -f 1,7 passwd
4.指定截取passwd文件中的第一列至第三列
[root@shell_example mnt]# cut -d : -f 1-3 passwd
5.指定截取passwd文件中的第1个字符和第4个字符
[root@shell_example mnt]# cut -c 1,4 passwd
6.指定截取passwd文件中的第1个字符至第4个字符
[root@shell_example mnt]# cut -c 1-4 passwd
- 练习:
1.编写一个脚本,可以自动检测系统中可登陆的用户
[root@shell_example mnt]# vim login_user.sh
#!/bin/bash
grep bash$ /etc/passwd | cut -d : -f 1
[root@shell_example mnt]# sh login_user.sh
root
student
2.执行ip_show.sh,显示当前主机的ip地址
[root@shell_example mnt]# vim ip_show.sh
#!/bin/bash
ifconfig eth0 | grep "inet" | cut -d " " -f 10
[root@shell_example mnt]# sh ip_show.sh
172.25.254.129
四、sort命令与uniq命令
1.sort命令
多用于字符排序
sort -n 纯数字排序
sort -r 倒序
sort -u 去掉重复数字
sort -o 输出到指定文件中
sort -t 指定分隔符
sort -k 指定要排序的列
2.uniq命令
对重复字符作相应的处理
uniq -u 显示唯一的行
uniq -d 显示重复的行
uniq -c 每行显示一次并统计重复次数
1.编辑westos并查看内容
[root@shell_example mnt]# vim westos
[root@shell_example mnt]# cat westos
2.将文件westos中的数字,进行排序
[root@shell_example mnt]# sort westos
3.将文件westos中的数字,从小到大进行排序
[root@shell_example mnt]# sort -n westos
4.将文件westos中的数字,从大到小进行排序
[root@shell_example mnt]# sort -rn westos
5.将文件westos中的数字,从大到小进行排序并将重复的删除
[root@shell_example mnt]# sort -run westos
6.将文件westos中的数字正向排序并在前方标出每个数字出现的次数
[root@shell_example mnt]# sort -n westos | uniq -c
7.将文件westos中的数字正向排序,只显示出现了多次的数字
[root@shell_example mnt]# sort -n westos | uniq -d
8.将文件westos中的数字正向排序,只显示出现了一次的数字
[root@shell_example mnt]# sort -n westos | uniq -u
9.对文件westos中的内容进行修改
[root@shell_example mnt]# vim westos
[root@shell_example mnt]# cat westos
10.对文件westos中的数字进行排序,发现字母比数字要前,且比较时主要看的是分隔符之前的字符
[root@shell_example mnt]# sort -n westos
11.对文件westos中的数字以“:"作为分隔符,并且指定排序的列为第二列
[root@shell_example mnt]# sort -t : -k 2 westos
12.对文件westos中的数字以“:"作为分隔符,并且指定排序的列为第二列,再加上-n排序后,就会按照从小到大的顺序正向排序
[root@shell_example mnt]# sort -t : -k 2 -n westos
四、&& 和 ||
&&用来执行条件成立后执行的命令
||用来执行条件不成立后执行的命令
例如:
ping -c1 -w1 172.25.254.128 && echo yes
ping -c1 -w1 172.25.254.128 || echo no
1.连接172.25.254.63可以连通,所以输出yes
[root@shell_example mnt]# ping -c1 -w1 172.25.254.63 &> /dev/null && echo yes || echo no
yes
[root@shell_example mnt]# ping 172.25.254.63
2.连接172.25.254.333(与本机ip在同一网段但不合法),不可以连通,所以输出no
[root@shell_example mnt]# ping -c1 -w1 172.25.254.333 &> /dev/null && echo yes || echo no
no
[root@shell_example mnt]# ping 172.25.254.333
注意:c=次数,w=运行时间 -c1 , -w1表示一分钟一次
五、test命令
test命令和[]等同
test "$A" == "$B" 等同 [ "$A" == "$B" ]
[ "$A" = "$B" ]
[ "$A != "B" ]
[ "$A" -eq "$B" ]
[ "$A" -ne "$B" ]
[ "$A" -le "$B" ]
[ "$A" -lt "$B" ]
[ "$A" -ge "$B" ]
[ "$A" -gt "$B" ]
[ "$A" -ne "$B" -a "$A" -gt "$B" ]
[ "$A" -ne "$B" -o "$A" -gt "$B" ]
[ -z "$A" ]
[ -n "$A" ]
[ "file1" -ef "file2" ]
[ "file1" -nt "file2" ]
[ "file1" -ot "file2" ]
1.输出变量a=1,b=1,比较a和b的值并利用&&和||输出结果,如果a和b的值相等,则输出yes,如果不相等,则输出no。
[root@shell_example mnt]# a=1
[root@shell_example mnt]# b=1
[root@shell_example mnt]# test "$a" = "$b" && echo yes || echo no
yes
2.变量a和b的值不相等所以输出no
[root@shell_example mnt]# a=3
[root@shell_example mnt]# b=1
[root@shell_example mnt]# test "$a" = "$b" && echo yes || echo no
no
因为test命令和[]等同,所以上述的命令也可写成以下命令:
[root@shell_example mnt]# [ "$a" = "$b" ] && echo yes || echo no
3.判断两个变量是否相等时可以用以下命令:
[root@shell_example mnt]# [ "$a" != "$b" ] && echo yes || echo no
[root@shell_example mnt]# [ ! "$a" = "$b" ] && echo yes || echo no
4.当两个变量相等时,可以使用以下命令进行判定
[root@shell_example mnt]# a=1
[root@shell_example mnt]# b=1
[root@shell_example mnt]# [ "$a" = "$b" ] && echo yes || echo no
yes
[root@shell_example mnt]# [ "$a" -eq "$b" ] && echo yes || echo no ##比较两个变量是否相等
yes
[root@shell_example mnt]# [ "$a" -ge "$b" ] && echo yes || echo no ##比较第一个变量是否大于或等于第二个变量
yes
[root@shell_example mnt]# [ "$a" -le "$b" ] && echo yes || echo no ##比较第一个变量是否小于或等于第二个变量
yes
5.当第一个变量小于第二个变量时,可以使用以下命令进行判定
[root@shell_example mnt]# a=2
[root@shell_example mnt]# b=3
[root@shell_example mnt]# [ "$a" -eq "$b" ] && echo yes || echo no ##第一个变量不等于第二个变量
no
[root@shell_example mnt]# [ "$a" -le "$b" ] && echo yes || echo no ##第一个变量小于等于第二个变量
yes
[root@shell_example mnt]# [ "$a" -lt "$b" ] && echo yes || echo no ##第一个变量小于第二个变量
yes
[root@shell_example mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
no
[root@shell_example mnt]# [ "$a" -gt "$b" ] && echo yes || echo no
no
6.当第一个变量大于第二个变量时,可以使用以下命令进行判定
[root@shell_example mnt]# a=5
[root@shell_example mnt]# b=3
[root@shell_example mnt]# [ "$a" -eq "$b" ] && echo yes || echo no
no
[root@shell_example mnt]# [ "$a" -le "$b" ] && echo yes || echo no
no
[root@shell_example mnt]# [ "$a" -lt "$b" ] && echo yes || echo no
no
[root@shell_example mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
yes
[root@shell_example mnt]# [ "$a" -gt "$b" ] && echo yes || echo no
yes
7.判断a中的内容是否为空,若为空,则输出yes,若不为空,则输出no
[root@shell_example mnt]# a=1
[root@shell_example mnt]# [ -z "$a" ] && echo yes || echo no
no
8.判断a中的内容是否为空,若为空,则输出yes,若不为空,则输出no
[root@shell_example mnt]# a=""
[root@shell_example mnt]# [ -z "$a" ] && echo yes || echo no
yes
9.查看文件的uid及属性
[root@shell_example mnt]# touch westos ##新建一个文件
[root@shell_example mnt]# vim westos ##在westos内写入内容
[root@shell_example mnt]# ls -li westos ##查看westos的uid及属性
[root@shell_example mnt]# touch westos1 ##新建一个文件westos1
[root@shell_example mnt]# ls -li ##查看mnt下新建的两个文件
[root@shell_example mnt]# rm -rf westos1 ##删除westos1
[root@shell_example mnt]# ls ##查看mnt下的文件
[root@shell_example mnt]# ln /mnt/westos /mnt/westos1 ##制作文件westos的一个链接文件westos1
[root@shell_example mnt]# ls -li ##查看链接文件及源文件的uid及属性,发现除了文件名不相同,其他都相同
10.比较两个文件的建立时间
[root@shell_example mnt]# [ "westos" -ef "westos1" ] && echo yes || echo no ##比较两个文件建立的时间是否相等
yes
[root@shell_example mnt]# [ "westos" -nt "westos1" ] && echo yes || echo no ##比较westos文件是否比westos1文件建立的时间新,因为是链接文件,所以不存在时间早晚
no
[root@shell_example mnt]# [ "westos" -ot "westos1" ] && echo yes || echo no ##比较westos文件是否比westos1文件建立的时间旧,因为是链接文件,所以不存在时间早晚
no
11.新建一个文件file1,比较两个文件建立时间
[root@shell_example mnt]# touch file1
[root@shell_example mnt]# [ "westos" -ef "file1" ] && echo yes || echo no ##比较两个文件的建立时间是否相同
no
[root@shell_example mnt]# [ "westos" -nt "file1" ] && echo yes || echo no ##比较westos文件是否比file1文件建立的时间新,查看结果可知westos比file1文件建立的时间旧
no
[root@shell_example mnt]# [ "westos" -ot "file1" ] && echo yes || echo no ##比较westos文件是否比file1文件建立的时间旧,查看结果可知westos比file1文件建立的时间旧
yes
六、查看文件的文件类型
[-e “file”] 查看文件是否存在
[-f “file”] 查看文件是否是文本文件
[-L “file”] 查看文件是否是链接文件
[-S “file”] 查看文件时都是套接字文件
[-b “file”] 查看文件是否是块设备文件
[-d “file”] 查看文件是否是目录
[-c “file”] 查看文件是否是字符设备文件
1.-e查看文件是否存在
[root@shell_example mnt]# ls
file1 westos westos1
[root@shell_example mnt]# [ -e "file1" ] && echo yes || echo no
yes
[root@shell_example mnt]# rm -rf file1
[root@shell_example mnt]# [ -e "file1" ] && echo yes || echo no
no
2.-f查看文件是否是文本文件
[root@shell_example mnt]# ln -s westos file1
[root@shell_example mnt]# ls -l
total 8
lrwxrwxrwx. 1 root root 6 May 24 13:41 file1 -> westos
-rw-r--r--. 2 root root 13 May 24 13:37 westos
-rw-r--r--. 2 root root 13 May 24 13:37 westos1
[root@shell_example mnt]# [ -f "file1" ] && echo yes || echo no
yes
3.-L 查看文件是否是链接文件
[root@shell_example mnt]# [ -L "file1" ] && echo yes || echo no
yes
4.-b查看文件是否是块设备文件
[root@shell_example mnt]# [ -b "/dev/vda" ] && echo yes || echo no
yes
5.-c查看文件是否是字符设备文件
[root@shell_example mnt]# [ -c "/dev/pts/0" ] && echo yes || echo no
yes
6.-d查看文件是否是目录
[root@shell_example mnt]# [ -d "/dev" ] && echo yes || echo no
yes
7.-S(查看文件时都是套接字文件)
[root@shell_example mnt]# yum install mariadb-server.x86_64 -y
[root@shell_example mnt]# systemctl start mariadb
[root@shell_example mnt]# ls /var/lib/mysql
aria_log.00000001 ibdata1 ib_logfile1 mysql.sock test
aria_log_control ib_logfile0 mysql performance_schema
[root@shell_example mnt]# ls /var/lib/mysql/mysql.sock
/var/lib/mysql/mysql.sock
[root@shell_example mnt]# [ -S "/var/lib/mysql/mysql.sock" ] && echo yes || echo no
yes
[root@shell_example mnt]# ls -l /var/lib/mysql/mysql.sock