文章目录
1.diff命令 用来比较两个文件或目录的不同
(1)使用格式
diff [options] target1 target2
diff file1 file2
diff diretcory1 directory2
(2)常用[options]
-b | 忽略空格字符 |
-B | 忽略空白行 |
-c | 显示全部内文,并标出不同处 |
-i | 忽略大小写 |
-p | 与-c相似 |
-q | 只显示有无差异,不显示详细内容 |
-r | 比较子目录文件 |
-u | 补丁,以合并的方式显示文件内容的不同 |
(3)输出读取
[num1,num2]a|c|d[num3,num4]
输出参数 | 含义 |
---|---|
num | 文件中的行数,左侧为第一个文件中的,右侧为第2个文件中的 |
a | add 添加 |
c | change 改变 |
d | delete 删除 |
-
2,4c2,4 表示改变第一个文件中的2,4行才能匹配第二个文件中的2,4行
-
diff -ci file1 file2
-
diff -r /mnt/dir1 /mnt/dir2
-
diff -q file1 file2
-
diff -B file1 file2
2.pacth 补丁 补全文件差异
yum install patch -y
patch -b
- diff -u file1 file2 > file.path
patch -b file1 file.path
3.cut 字符截取
(1)使用格式: cut [options] file
(2)常用options
-d | 指定分隔符 |
-f | 指定截取的列 |
-c | 指定截取的字符位置 |
3.sort 字符排序
(1)使用格式: sort [options] file
(2)常用[options]
-n | 纯数字排序 |
-r | 倒序 |
-u | 去掉重复数字 |
-t | 指定分隔符 |
-k | 指定要排序的列 |
-o | 输出到指定文件中 |
- sort -nru file
- sort -t : -k
4.uniq 对重复字符做相应的处理
命令 | 含义 |
---|---|
uniq -u | 显示唯一的行 |
uniq -d | 显示重复的行 |
uniq -c | 每行显示一次并统计重复次数 |
5.&&与||
&&执行条件成立后执行的命令
|| 执行条件不成立后执行的命令
6.test命令
test 等同于 [ ]
test " $A"=="$B"
等同于 [“
A
"
=
"
A"="
A"="B”]
数值比较:
[ "$A" = "$B" ] A、B是否相等
[ "$A" != "$B" ] A、B是否不相等
[ "$A" -eq "$B" ] (equal) A、B是否相等
[ "$A" -ne "$B" ] (not equal) A、B是否不相等
[ "$A" -ge "$B" ] (greater and equal )A是否大于等于B
[ "$A" -gt "$B" ] (greater than)A 是否大于B
[ "$A" -le "$B" ] (less and equal)A是否小于小于等于B
[ "$A" -lt "$B" ] (less than)A是否小于B
检查对象是否存在:
[ -z "$A" ] 参数$A是否为空
[ -n "$A" ] 参数$A是否不为空
检查文件:
[ -e “file” ] | 文件是否存在 |
[ -f “file” ] | 文件是否为公共文件 |
[ -L “file”] | 文件是否为软连接 |
[ -S “file” ] | 文件是否为套接字 |
[ -b “file” ] | 文件是否为块设备 |
[ -d “file” ] | 文件是否为目录 |
[ -c “file” ] | 文件是否为字符设备 |
[ “file1” -ef “file2” ] | 两文件节点数是否相等 |
[ “file1” -nf “file2” ] | 两个文件哪个建立的早 |
[ “file1” -ot “file2” ] | 两个文件哪个建立的晚 |
[root@foundation70 mn]# cd /dev/pts
[root@foundation70 pts]# ls
0 ptmx
[root@foundation70 pts]# [ -c "/dev/pts/0" ]&& echo yes || echo no
yes
[root@foundation70 pts]# ll /dev/pts/0
crw--w----. 1 kiosk tty 136, 0 Aug 20 21:58 /dev/pts/0
[root@foundation70 pts]# ln -s /mn/file /mnt/test
[root@foundation70 mn]# ls
file file1 test test.sh
[root@foundation70 mn]# ll
total 12
-rw-r--r--. 1 root root 20 Aug 20 21:02 file
-rw-r--r--. 1 root root 15 Aug 20 21:02 file1
lrwxrwxrwx. 1 root root 8 Aug 20 22:00 test -> /mn/file
-rwxr-xr-x. 1 root root 159 Aug 20 21:51 test.sh
[root@foundation70 mn]# [ -L "/mn/test" ]&& echo yes || echo no
yes
[root@foundation70 mn]# [ -e "/mn/test" ]&& echo yes || echo no
yes
[root@foundation70 mn]# [ -f "/mn/test" ]&& echo yes || echo no
yes
[root@foundation70 mn]# [ -b "/mn/test" ]&& echo yes || echo no
no
[root@foundation70 mn]# [ -b "/dev/sda1" ]&& echo yes || echo no
yes
[root@foundation70 mn]# [ -d "/dev/sda1" ]&& echo yes || echo no
no
多条件的连接:
[ "$A" -gt "0" -a "$A" -le "10" ] and 且 ##$A大于0且小于等于10,两个条件同时满足
[ "$A" -gt "10" -o "$A" -le "0" ] or 或 ##$A大于10或小于0 ,二者满足一项就好
7.tr 大小写转换
- echo hello HELLO | tr ‘a-z’ ‘A-Z’
- echo hello HELLO | tr ‘A-Z’ ‘a-z’
8. 牛刀小试
(1)编写简单脚本:当/的挂载比重超过10%,日志中就会生成警告
[root@foundation70 mn]# df / | tail -n 1 | cut -d " " -f 6
12%
[root@foundation70 mn]# df / | tail -n 1 | cut -d " " -f 6 |cut -d % -f 1
12
[root@foundation70 mn]# vim test.sh
1 #!/bin/bash
2 [ `df / | tail -n 1 | cut -d " " -f 6 |cut -d % -f 1` -ge "10" ] && {
3 logger Warning: System root is full!!
4 }
5 at now+1min <<EOF
6 /mn/test.sh
7 EOF
~
[root@foundation70 mn]# vim test.sh
[root@foundation70 mn]# chmod +x test.sh
[root@foundation70 mn]# ./test.sh
job 12 at Tue Aug 20 21:53:00 2019
[root@foundation70 mn]# at -l
12 Tue Aug 20 21:53:00 2019 a root
[root@foundation70 mn]# > /var/log/messages
[root@foundation70 mn]# date
Tue Aug 20 21:53:53 CST 2019
[root@foundation70 mn]# cat /var/log/messages
Aug 20 21:53:00 foundation70 systemd: Created slice user-0.slice.
Aug 20 21:53:00 foundation70 systemd: Starting user-0.slice.
Aug 20 21:53:00 foundation70 systemd-logind: New session 93 of user root.
Aug 20 21:53:00 foundation70 systemd: Started Session 93 of user root.
Aug 20 21:53:00 foundation70 systemd: Starting Session 93 of user root.
Aug 20 21:53:00 foundation70 root: Warning: System root is full!!