一.diff
1.diff命令
diff命令是用来比较俩个文件的或者目录的不同
diff [options] target1 target2
diff file1 file2
diff directory1 directory2
2.diff在文件比较过程中的读取方式
[num1,num2][a][c][d][num3][num4]
num1,num2表示在第一个文件中的行数
num3,num4表示在第一个文件中的行数
a表示添加-------add
c表示更改-------change
d表示删除-------delete
< 表示第一个文件中的内容
表示第二个文件中的内容
3.diff命令中常用的参数
-b或者–ignore-space-change | 不检查空格字符的不同 |
---|---|
-B或–ignore-blank-lines | 不检查空白行 |
-c | 显示全部内文,并标出不同之处 |
-c1 | 显示不同字符的上下1行 |
-c2 | 显示不同字符的上下俩行 |
-i或–ignore-case | 不检查大小写的不同 |
-p | 若比较的文件为c语言的程序文件时,显示差异所在的函数名称 |
-q或–brief | 仅显示有无差异,不显示详细信息 |
-r或–recursive | 比较子目录中的文件 |
-u | 以合并的方式来显示文件内容的不同 |
[root@shell mnt]# diff -b test1 test2
[root@shell mnt]# diff -B test1 test2
[root@shell mnt]# diff -c test1 test2
[root@shell mnt]# diff -i test1 test2
[root@shell mnt]# diff -q test1 test2
[root@shell mnt]# diff -u test1 test2
二.patch命令
用于对不同的文件打补丁
1.patch的启用
yum install patch -y ##在yum源搭建好后
2.patch的使用
[root@shell ~]# diff -u test test1 > test.path ##比较文件的全文的不同,并定向到补丁文件test.path中
[root@shell ~]# patch test test.patch ##生成新的文件test.path
[root@shell ~]# patch -b test test.patch ##-b表示生成新的文件test.orig但不覆盖源文件test.path的值
三.cut命令
cut命令多用于字符截取
cut常用命令及其参数
cut -d | 指定分隔符 |
---|---|
cut -f | 指定截取的列 |
cut -c | 指定截取的字符位置 |
[root@shell mnt]# cut -d : -f 1 passwd ##截取该文件第一列
[root@shell mnt]# cut -d : -f 1-3 passwd
[root@shell mnt]# cut -c 1-4 passwd ##截取第1到4个字符
四.sort命令
sort多用于字符排序
sort -n | 纯数字排序 |
---|---|
sort -r | 倒序 |
sort -u | 去掉重复数字 |
sort -o | 输出到指定文件中 |
sort -t | 指定分割符 |
sort -k | 指定要排序的列 |
[root@shell mnt]# sort -rn westos ##倒序且纯数字排序
[root@shell mnt]# sort -run westos ##纯数字倒序并且去掉重复数字
五.uniq命令
对重复的字符做相应处理
uniq -u | 显示唯一的行 |
---|---|
uniq -d | 显示重复的行 |
uniq -c | 每行显示一次并统计重复次数 |
[root@shell mnt]# sort -n westos | uniq -c
1 23 ##23出现了1次
[root@shell mnt]# sort -n westos | uniq -d
[root@shell mnt]# sort -n westos | uniq -u
[root@shell mnt]# sort -t : -k 2 westos
[root@shell mnt]# sort -t : -k 2 -n westos
六.&&和||
&&用来执行条件成立后执行的命令
||用来执行条件不成立后执行的命令
七.test命令
test命令和[ ]等同
tset “$ A"=="$ B"等同[ “$A” = = " $ B "
-ef | 俩个文件是不是属于同一个 |
---|---|
-nt | 前一个文件比后一个文件新 |
-ot | 前一个文件比后一个文件老 |
inode | id的节点号 |
-e | 文件是否存在 |
-f | 是否为文件 |
-L | 是否为链接 |
-b | 是否为块设备 |
-c | 是否为字符设备 |
-d | 是否为目录 |
-s | 是否为套接字 |
[ "$A="$B"] [ "$A -eq "$B"] #A等于B
[ "$A!="$B"] [ "$A -ne "$B"] #A不等于B
[ "$A" -le "$B" ] #A小于等于B
[ "$A" -lt "$B" ] #A小于B
[ "$A" -ge "$B" ] #A大于等于B
[ "$A" -gt "$B" ] #A大于B
[ "$A" -ne "$B" -a "$A" -gt "$B" ] #A不等于B并且A大于B
[ "$A" -ne "$B" -o "$A" -gt "$B" ] #A不等于B或者A大于B
[ -z "$A ] #判断A是否为0
[ -n "$A ] #判断A是否存在
[ "file1" -ef "file2" ] ##file1和file2是否为同一个文件
[ "file1" -nt "file2" ] ##file1比file2建立的时间晚
[ "file1" -ot "file2" ] ##file1比file2建立的时间早
练习:
(1)在/mnt写一个脚本,执行脚本时脚本后面如果输入为目录则显示这是一个目录,如果输入一个文件时则显示这是一个文件,如果不存在是显示不存在,如果输入为空时则显示要输入的内容,如果以上都不符合则exit脚本退出,脚本运行的程序立即终止
[root@shell mnt]# vim check_file.sh
文件编辑内容如下:
验证如下:
(2)在/mnt下写一个脚本,后面如果加ip如果ping通则显示yes,否则显示no,空的话提示要输入的检测的ip
[root@shell mnt]# vim ping.sh
脚本编辑内容如下:
检测如下: