[root@trade shell]# cat yan
123
4
1
2
3
7
81
13
81
13
81
81
123
4
123
[root@trade shell]# uniq -c yan
1 123
1 4
1 1
1 2
1 3
1 7
1 81
1 13
1 81
1 13
2 81
1 123
1 4
1 123
[root@docker3 mnt]# sort -n yan | uniq -c
1 2
1 3
1 4
1 7
1 13
2 81
1 123
[root@docker3 mnt]# sort -n yan | uniq -d
81
[root@docker3 mnt]# sort -n yan | uniq -u
1
2
3
4
7
13
123
[root@trade shell]# cat yan
123
4
1
2
3
7
81
13
81
13
81
81
123
4
123
[root@trade shell]# sort -n yan | uniq -c
1 1
1 2
1 3
2 4
1 7
2 13
4 81
3 123
[root@trade shell]# sort -n yan | uniq -c | sort -n -k 1
1 1
1 2
1 3
1 7
2 13
2 4
3 123
4 81
[root@trade shell]# sort -n yan | uniq -c | sort -n -k 1 | tail -n1
4 81
[root@trade shell]# sort -n yan | uniq -c | sort -n -k 1 | tail -n 1 | cut -d " " -f 8
81
[kiosk@foundation38 Desktop]$ ifconfig wlan0
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.31.177 netmask 255.255.255.0 broadcast 192.168.31.255
inet6 fe80::cd76:22a3:151a:9e8d prefixlen 64 scopeid 0x20<link>
ether b0:68:e6:99:ba:7d txqueuelen 1000 (Ethernet)
RX packets 34279 bytes 33490309 (31.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 21281 bytes 5558502 (5.3 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[kiosk@foundation38 Desktop]$ ifconfig wlan0 | head -n 2
wlan0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.31.177 netmask 255.255.255.0 broadcast 192.168.31.255
[kiosk@foundation38 Desktop]$ ifconfig wlan0 | head -n 2 | tail -n 1
inet 192.168.31.177 netmask 255.255.255.0 broadcast 192.168.31.255
[kiosk@foundation38 Desktop]$ ifconfig wlan0 | head -n 2 | tail -n1 | cut -d " " -f 10
192.168.31.177
tr用法
tr ‘a-z’ ‘A-Z’ 小写转换成大写
tr ‘A-Z’ ‘a-z’ 大写转换成小写
[root@docker3 mnt]# cat yan
hello
HELLO
[root@docker3 mnt]# tr 'l' 's' < yan
hesso
HELLO
[root@docker3 mnt]# tr 'a-z' 'A-Z' < yan
HELLO
HELLO
[root@docker3 mnt]# cat yan
hello
HELLO
&&条件成立则…(是)
||不成立则…(否)
[root@docker3 mnt]# ping -c1 -w1 172.25.254.3 &> /dev/null && echo 172.25.254.3 is up || echo 172.25.254.3 is down
172.25.254.3 is up
[root@docker3 mnt]# ping -c1 -w1 172.25.254.5 &> /dev/null && echo 172.25.254.5 is up || echo 172.25.254.5 is down
172.25.254.5 is down
test是作比较的
test = [ ]
“test $a = $b” = [ " $a" = " $b" ]
test数字对比
=
!=
-eq 等于
-ne 不等于
-le 小于等于
-lt 小于
-ge 大于等于
-gt 大于
[root@docker3 mnt]# a=1
[root@docker3 mnt]# b=1
[root@docker3 mnt]# test "$a" = "$b" && echo yes || echo no
yes
[root@docker3 mnt]# a=2
[root@docker3 mnt]# test "$a" = "$b" && echo yes || echo no
no
[root@docker3 mnt]# [ "$a" = "$b" ] && ehco yes || echo no
no
[root@docker3 mnt]# [ ! "$a" = "$b" ] && echo yes || echo no
yes
[root@docker3 mnt]# [ ! "$a" -eq "$b" ] && echo yes || echo no
yes
[root@docker3 mnt]# [ ! "$a" -ne "$b" ] && echo yes || echo no
no
[root@docker3 mnt]# [ "$a" -ne "$b" ] && echo yes || echo no
yes
[root@docker3 mnt]# [ "$a" -le "$b" ] && echo yes || echo no
no
[root@docker3 mnt]# [ "$a" -lt "$b" ] && echo yes || echo no
no
[root@docker3 mnt]# [ "$a" -ge "$b" ] && echo yes || echo no
yes
[root@docker3 mnt]# [ "$a" -gt "$b" ] && echo yes || echo no
yes
-a 并且
-o 或者
[root@docker3 mnt]# [ "$a" -gt "0" -a "$a" -lt "10" ] && echo yes || echo no
yes
[root@docker3 mnt]# a=20
[root@docker3 mnt]# [ "$a" -gt "0" -a "$a" -lt "10" ] && echo yes || echo no
no
[root@docker3 mnt]# [ "$a" -gt "0" -o "$a" -lt "10" ] && echo yes || echo no
yes
test对空的判定
-n nozero判定内容不为空
-z zero判定内容为空
[root@docker3 mnt]# [ -z "$c" ] && echo yes || echo no
yes
[root@docker3 mnt]# [ -n "$c" ] && echo yes || echo no
no
user_check.sh 用户
用户类型为
super user 0
system user 1-999
common user
$*表示脚本后面跟的所有字符串
[root@docker3 mnt]# sh user_check.sh
error : Please input username
[root@docker3 mnt]# sh user_check.sh yan
yan is common user
[root@docker3 mnt]# sh user_check.sh root
root is super user
[root@docker3 mnt]# sh user_check.sh out
ERROR: out is not exist
test对于文件类型的判定
-ef 文件节点号是否一致(硬链)
-nt 文件1是否比文件2新
-ot 文件1是否比文件2老
-d 目录
-S 套接字
-L 软连接
-e 存在
-f 普通文件
-b 快设备
-c 字符设备
[root@docker3 mnt]# [ -e "/mnt" ] && echo yes || echo no
yes
[root@docker3 mnt]# [ -d "/mnt" ] && echo yes || echo no
yes
[root@docker3 mnt]# [ -S "/mnt" ] && echo yes || echo no
no
[root@docker3 mnt]# [ -L "/mnt" ] && echo yes || echo no
no
[root@docker3 mnt]# [ -f "/mnt" ] && echo yes || echo no
no
[root@docker3 mnt]# [ -c "/mnt" ] && echo yes || echo no
no
[root@docker3 mnt]# [ "/mnt/yan" -ef "/mnt/yan1" ] && echo yes || echo no
no
[root@docker3 mnt]# [ "/mnt/yan" -ef "/mnt/yan2" ] && echo yes || echo no
yes
[root@docker3 mnt]# [ "/mnt/yan" -ot "/mnt/file" ] && echo yes || echo no
no
[root@docker3 mnt]# [ "/mnt/yan" -nt "/mnt/file" ] && echo yes || echo no
yes
执行脚本
file_check.sh在执行时
如果脚本后未指定检测文件报错“未检测文件,请指定”
如果脚本后指定文件不存在报错“此文件不存在”
当文件存在时请检测文件类型并显示到输出中
END
Usage: diff [OPTION] files|dir
输出信息:
【num1,num2】a|c|d 【num3,num4】
之前是第一个文件的行
之后是第二个文件的行
a 添加
c 更改
d 删除
<第一个文件的内容
">"第二个文件的内容
[root@docker3 mnt]# vim yan
[root@docker3 mnt]# cat yan > yan1
[root@docker3 mnt]# ls
yan yan1
[root@docker3 mnt]# vim yan1
[root@docker3 mnt]# cat yan
hello yan
[root@docker3 mnt]# cat yan1
hello yan
123
[root@docker3 mnt]# diff yan yan1
1a2
> 123
[root@docker3 mnt]# vim yan
[root@docker3 mnt]# cat yan
hello yan
456
[root@docker3 mnt]# cat yan1
hello yan
123
[root@docker3 mnt]# diff yan yan1
2c2
< 456
---
> 123
[root@trade shell]# diff yan yan1
1,2c1,2
< hello yan
< 123
---
> Ahello yan
> 456
[root@docker3 mnt]# cat yan
Ahello yan
456
123
[root@docker3 mnt]# cat yan1
hello yan
123
000
[root@docker3 mnt]# diff yan yan1
1,2c1
< Ahello yan
< 456
---
> hello yan
3a3
> 000
[root@docker3 mnt]# cat yan
hello yan
[root@docker3 mnt]# cat yan1
hello yan
123
[root@docker3 mnt]# diff yan1 yan
2d1
< 123
[root@trade shell]# cat yan
yan
[root@trade shell]# cat yan1
yan
123
[root@trade shell]# diff yan yan1
1a2
> 123
[root@trade shell]# diff yan1 yan
2d1
< 123
常用参数
-b 忽略空格 | -B忽略空行 |
---|---|
-i忽略大小写 | -c显示文件所有内容并标识不同 |
-r对比目录 | -u合并输出 |
[root@docker3 mnt]# cat yan
hello yan
[root@docker3 mnt]# cat yan1
hello yan
[root@docker3 mnt]# diff yan yan1
1c1
< hello yan
---
> hello yan
[root@docker3 mnt]# diff -b yan yan1
[root@docker3 mnt]# cat yan
hello yan
空行
[root@docker3 mnt]# cat yan1
hello yan
[root@docker3 mnt]# diff yan yan1
2d1
<
[root@docker3 mnt]# diff -B yan yan1
[root@docker3 mnt]# cat yan
Hello yan
[root@docker3 mnt]# cat yan1
hello yan
[root@docker3 mnt]# diff yan yan1
1c1
< Hello yan
---
> hello yan
[root@docker3 mnt]# diff -i yan yan1
[root@docker3 mnt]# cat yan
123
456
Hello yan
123
1234
[root@docker3 mnt]# cat yan1
123
456
Hello yan
o23
1234
[root@docker3 mnt]# diff yan yan1
4c4
< 123
---
> o23
[root@docker3 mnt]# diff -c yan yan1
*** yan 2022-02-17 06:33:56.176000000 +0800
--- yan1 2022-02-17 06:34:12.207000000 +0800
***************
*** 1,5 ****
123
456
Hello yan
! 123
1234
--- 1,5 ----
123
456
Hello yan
! o23
1234
[root@docker3 mnt]# mkdir yan2 yan3
[root@docker3 mnt]# touch yan2/yan
[root@docker3 mnt]# diff -r yan2 yan3
Only in yan2: yan
[root@docker3 mnt]# cat yan
123
456
Hello yan
123
1234
[root@docker3 mnt]# cat yan1
123
456
Hello yan
o23
[root@docker3 mnt]# diff -u yan yan1
--- yan 2022-02-17 06:33:56.176000000 +0800
+++ yan1 2022-02-17 06:34:12.207000000 +0800
@@ -1,5 +1,5 @@
123
456
Hello yan
-123
+o23
1234
[root@docker3 mnt]# diff -u yan yan1 > yan.path
path用法 打补丁
patch 原文件 补丁文件
-b 备份原文件
[root@docker3 mnt]# yum install patch -y
[root@docker3 mnt]# patch -b yan yan.path
[root@docker3 mnt]# cat yan
123
456
Hello yan
o23
1234
[root@docker3 mnt]# cat yan.path
--- yan 2022-02-17 06:33:56.176000000 +0800
+++ yan1 2022-02-17 06:34:12.207000000 +0800
@@ -1,5 +1,5 @@
123
456
Hello yan
-123
+o23
1234
[root@docker3 mnt]# cat yan.orig -b备份的原文件
123
456
Hello yan
123
1234
cut
-d : 指定:为分割符
-f 指定显示的列 5第五列 | 3,5第三列和第五列 | 3-5第三列到第五列 | 5- 第五列以后 | -5 到第五列
-c 指定截取字符,(用数字用法和-f一样)
取出第五行
[root@docker3 mnt]# cp /etc/passwd .
[root@docker3 mnt]# head -n 5 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
[root@docker3 mnt]# head -n 5 passwd | tail -n1
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
取出第五列
[root@docker3 mnt]# cut -d : -f 5 passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
games
第一列和第五列
[root@docker3 mnt]# cut -d : -f 1,5 passwd
root:root
bin:bin
daemon:daemon
adm:adm
lp:lp
sync:sync
shutdown:shutdown
halt:halt
games:games
[root@docker3 mnt]# cut -c1-4 passwd
root
bin:
daem
adm:
lp:x
sync
shut
halt
game
tss:
-n 纯数字排序
-r 倒序
-u 去掉重复
-o 输出到指定文件
-t 指定分割符
-k 指定排序的列
[root@docker3 mnt]# sort yan
1
123
13
2
3
4
7
81
81
[root@docker3 mnt]# sort -n yan
1
2
3
4
7
13
81
81
123
[root@docker3 mnt]# sort -rn yan
123
81
81
13
7
4
3
2
1
[root@docker3 mnt]# sort -rn yan -o hello
[root@docker3 mnt]# cat hello
123
81
81
13
7
4
3
2
1
[root@docker3 mnt]# sort -n yan
a:1
a:123
a:13
a:2
a:3
a:4
a:7
a:81
a:81
[root@docker3 mnt]# sort -t : -k 2 -n yan
a:1
a:2
a:3
a:4
a:7
a:13
a:81
a:81
a:123
[root@docker3 mnt]# sort -un yan
1
2
3
4
7
13
81
123
END