1、diff
输出信息:
[num1,num2][a|c|d][num3,num4]
num1,num2 ##第一个文件中的行
a ##添加
c ##更改
d ##删除
< ##第一个文件中的内容
> ##第二个文件中的内容
num3,num4 ##第二个文件中的行
常用参数:
-b ##忽略空格
-B ##忽略空行
-i ##忽略大小写
-c ##显示文件所有内容并标示不同
-r ##对比目录
-u ##合并输出
[root@localhost mnt]# cat westos
hello westos
[root@localhost mnt]# cat westos1
hello westos
123
[root@localhost mnt]# diff westos westos1
1a2
> 123
[root@localhost mnt]# vim westos1
hello westos
123
[root@localhost mnt]# diff westos westos1
1c1,2
< hello westos
---
> hello westos
> 123
[root@localhost mnt]# diff -b westos westos1
1a2
> 123
[root@localhost mnt]# cat westos1
hello westos
[root@localhost mnt]# cat westos
hello westos
[root@localhost mnt]# diff westos westos1
0a1
>
[root@localhost mnt]# diff -B westos westos1
[root@localhost mnt]# cat westos
hello westos
[root@localhost mnt]# cat westos1
Hello westos
[root@localhost mnt]# diff westos westos1
1c1
< hello westos
---
> Hello westos
[root@localhost mnt]# diff -i westos westos1
[root@localhost mnt]# cat westos
hello westos
123
[root@localhost mnt]# cat westos1
Hello westos
123
[root@localhost mnt]# diff -c westos westos1
*** 1,2 ****
! hello westos
123
--- 1,2 ----
! Hello westos
123
[root@localhost mnt]# mkdir westosdir westosdir1
[root@localhost mnt]# touch westosdir/westosfile
[root@localhost mnt]# diff -r westosdir westosdir1
Only in westosdir: westosfile
[root@localhost mnt]# diff -u westos westos1 > westos.path
[root@localhost mnt]# cat westos.path
@@ -1,2 +1,2 @@
-hello westos
+Hello westos
123
2、patch
#patch 原文件 补丁文件
#-b ##备份原文件
[root@localhost mnt]# dnf install -y patch
[root@localhost mnt]# vim westos1
hello westos
123
[root@localhost mnt]# vim westos
hello linux
456
[root@localhost mnt]# diff -u westos westos1 > westos.path
[root@localhost mnt]# patch -b westos westos.path
patching file westos
[root@localhost mnt]# ls
westos westos1 westosdir westosdir1 westos.orig westos.path
[root@localhost mnt]# cat westos
hello westos
123
[root@localhost mnt]# cat westos1
hello westos
123
[root@localhost mnt]# cat westos.path
--- westos 2021-07-01 20:34:53.547000000 +0800
+++ westos1 2021-07-01 20:34:38.728000000 +0800
@@ -1,2 +1,2 @@
-hello linux
-456
+hello westos
+123
[root@localhost mnt]# cat westos.orig
hello linux
456
3、cut
-d : ##指定:为分隔符
-f ##指定显示的列 5第五列| 3,5 3和5列|3-5 3到5列|5- 第五列以后|-5 到第五列
-c ##指定截取的字符(数字用法同-f)
[root@localhost mnt]# cut -d : -f 1 passwd
sssd
flatpak
colord
gdm
rpcuser
gnome-initial-setup
[root@localhost mnt]# cut -d : -f 1,4 passwd
sssd:978
flatpak:977
colord:976
gdm:42
rpcuser:29
gnome-initial-setup:975
[root@localhost mnt]# cut -d : -f 1-4 passwd
sssd:x:980:978
flatpak:x:979:977
colord:x:978:976
gdm:x:42:42
rpcuser:x:29:29
gnome-initial-setup:x:977:975
[root@localhost mnt]# cut -d : -f 5- passwd
User for sssd:/:/sbin/nologin
User for flatpak system helper:/:/sbin/nologin
User for colord:/var/lib/colord:/sbin/nologin
:/var/lib/gdm:/sbin/nologin
RPC Service User:/var/lib/nfs:/sbin/nologin
:/run/gnome-initial-setup/:/sbin/nologin
[root@localhost mnt]# cut -d : -f -2 passwd
sssd:x
flatpak:x
colord:x
gdm:x
rpcuser:x
gnome-initial-setup:x
[root@localhost mnt]# cut -c 1-4 passwd
sssd
flat
colo
gdm:
rpcu
gnom
[root@localhost mnt]# grep bash -v /etc/passwd | cut -d : -f 1
bin
daemon
adm
lp
sync
shutdown
halt
mail
4、sort
-n ##纯数字排序
-r ##倒叙
-u ##去掉重复
-o ##输出到指定文件
-t ##指定分隔符
-k ##指定排序的列
[root@localhost mnt]# sort -n westos
1
2
5
6
8
8
9
9
12
26
68
68
[root@localhost mnt]# sort -nr westos
68
68
26
12
9
9
8
8
6
5
2
1
[root@localhost mnt]# sort -nr westos -o westos1
[root@localhost mnt]# cat westos1
68
68
26
12
9
9
8
8
6
5
2
1
[root@localhost mnt]# sort -u westos
1
12
2
26
5
6
68
8
9
[root@localhost mnt]# sort -n westos ##默认按照第一列排序
0:68
0:8
0:8
0:9
1:9
2:12
2:2
2:6
5:5
6:1
8:26
8:68
[root@localhost mnt]# sort -t : -k 2 westos
6:1
2:12
2:2
8:26
5:5
2:6
0:68
8:68
0:8
0:8
0:9
1:9
[root@localhost mnt]# sort -n -t : -k 1 westos
6:1
2:2
5:5
2:6
0:8
0:8
0:9
1:9
2:12
8:26
0:68
8:68
5、uniq
-c #合并重复并统计重复个数
-d #显示重复的行
-u #显示唯一的行
[root@localhost mnt]# uniq -c westos
1 1
1 12
1 2
1 26
1 5
1 6
1 68
2 8
1 9
[root@localhost mnt]# uniq -d westos
8
[root@localhost mnt]# uniq -u westos
1
12
2
26
5
6
68
9
[root@localhost mnt]# sort -n westos | uniq -c | sort -k 1 -nr | cut -d " " -f 8 | head -n 1
9
[root@localhost mnt]# sort -n westos | uniq -c | sort -nrt " " -k 7 | cut -d " " -f 8 | head -n 1
9
命令测试:
1.ifconfig 网卡 可以显示此网卡的信息
显示信息中包含此网卡使用的ip地址
请用命令过滤此ip并在输出时只显示ip其他信息不显示
[root@localhost mnt]# ifconfig enp1s0 | head -n 2 | tail -n 1 | cut -d " " -f 10
1.1.1.88
2.找出能登陆系统用户中UID最大的用户,并显示其名称
[root@localhost mnt]# grep bash /etc/passwd | sort -t : -k3 -n | tail -n 1 | cut -d : -f 1
3.当前主机为web服务器,请抓取访问web服务器次数排在前5的ip地址
[root@localhost mnt]# cut -d " " -f 1 /etc/httpd/logs/access_log* | sort -n | uniq -c | sort -nr | head -n 5 | cut -d " " -f 7
[root@localhost mnt]# cut -d " " -f 1 /etc/httpd/logs/access_log* | sort -n | uniq -c | sort -nr | head -n 5 | sed 's/^ * //g' | cut -d " " -f 2
172.25.254.12
172.25.254.212
172.25.254.13
6、 tr
tr 'a-z' 'A-Z' ##小写转大写
tr 'A-Z' 'a-z' ##大写转小写
[root@localhost mnt]# ifconfig enp1s0 | head -2 | tail -1 | cut -d " " -f 10
1.1.1.88
[root@localhost mnt]# ifconfig enp1s0 | head -2 | tail -1 | cut -d " " -f 10 | tr '1' '*'
*.*.*.88
[root@localhost mnt]# ls
passwd westos
[root@localhost mnt]# ls | tr 'a-z' 'A-Z'
PASSWD
WESTOS
7、test、&&、 ||
&& 符合条件作动作
|| 不符合条件作动作
test = [] ##[] 就相当于test命令
"test $a = $b" = [ "$a" = "$b" ]
test数字对比
=
!=
-eq ##等于
-ne ##不等于
-le ##小于等于
-lt ##小于
-ge ##大于等于
-gt ##大于
test的条件关系
-a ##并且
-o ##或者
test对空的判定
-n ##nozero 判定内容不为空
-z ##zero 判定内容为空
[root@node1 ~]# ls /mnt/hello
ls: cannot access '/mnt/hello': No such file or directory
[root@node1 ~]# ls /mnt/hello &> /dev/null && echo yes || echo no
no
[root@node1 ~]#ls /mnt &> /dev/null && echo yes || echo no
yes
[root@node1 ~]# a=1
[root@node1 ~]# b=1
[root@node1 ~]# test "$a" = "$b" && echo yes || echo no
yes ## =等于 != 不等于
[root@node1 ~]# [ "$a" -eq "$b" ] && echo yes || echo no ##等于
[root@node1 ~]# [ "$a" -ne "$b" ] && echo yes || echo no ##不等于
[root@node1 ~]# [ "$a" -le "$b" ] && echo yes || echo no ##小于等于
[root@node1 ~]# [ "$a" -ge "$b" ] && echo yes || echo no ##大于等于
[root@node1 ~]# [ "$a" -lt "$b" ] && echo yes || echo no ##小于
[root@node1 ~]# [ "$a" -gt "$b" ] && echo yes || echo no ##大于
[root@node1 ~]# [ "$a" -gt "0" -a "$a" -lt "10" ] && echo yes || echo no ##-a 并且
[root@node1 ~]# [ "$a" -le "0" -o "$a" -ge "1" ] && echo yes || echo no ##-o 或者
[root@node1 ~]# [ "$a" -gt "0" ] && [ "$a" -lt "10" ] && echo yes || echo no
[root@node1 ~]# [ "$a" -gt "0" ] || [ "$a" -lt "1" ] && echo yes || echo no
[root@node1 ~]# [ -z "$c" ] && echo yes || echo no ##zero 判定内容为空 c未设定值
yes
[root@node1 ~]# [ -n "$c" ] && echo yes || echo no ##nozero 判定内容不为空
no
执行下列脚本来判断用户类型
user_check.sh 用户
- 用户类型为
super user
system user
common user
[root@node1 ~]# vim test.sh
#!/bin/bash
[ -z "$1" ] && {
echo "Error: Please input username following script!"
exit
}
id $1 &> /dev/null || {
echo "user $1 is not exist !"
exit
}
USER_UID=$(id -u $1)
USER_SHELL=$( grep $1 /etc/passwd | cut -d : -f 7)
[ "$USER_UID" -eq "0" ] && {
echo $1 is supper user
exit
}
[ "$USER_UID" -lt "1000" ] && [ ! "$USER_SHELL" = "/bin/bash" ] && {
echo $1 is systemuser
exit
}
[ "$USER_UID" -ge "1000" ] && [ "$USER_SHELL" = "/bin/bash" ] && {
echo $1 is common user
exit
}
echo "unknow user type !!"
[root@westoslinux mnt]# sh test.sh linux
user linux is not exist !
[root@westoslinux mnt]# sh test.sh westos
westos is common user
[root@westoslinux mnt]# sh test.sh root
root is supper user
[root@westoslinux mnt]# sh test.sh lee
lee is common user
test对于文件的判定
-ef ##文件节点号是否一致(硬链)
-nt ##文件1是不是比文件2新
-ot ##文件1是不是比文件2老
-d ##目录
-S ##套结字
-L ##软连接
-e ##存在
-f ##普通文件
-b ##快设备
-c ##字符设备
[root@node1 mnt]# [ "/mnt/westos" -ef "/mnt/westos1" ] && echo yes || echo no
no
[root@node1 mnt]# ls
westos
[root@node1 mnt]# ln /mnt/westos /mnt/file
[root@node1 mnt]# ls -i
17080711 file 17080711 westos
[root@node1 mnt]# [ "/mnt/westos" -ef "/mnt/file" ] && echo yes || echo no
yes
[root@node1 mnt]# [ "/mnt/westos" -nt "/mnt/westos1" ] && echo yes || echo no
yes
[root@node1 mnt]# [ "/mnt/westos" -ot "/mnt/westos1" ] && echo yes || echo no
no
[root@node1 mnt]# [ -d "/mnt/" ] && echo yes || echo no
yes
[root@node1 mnt]# ln -s /mnt/westos file1
[root@node1 mnt]# [ -L "/mnt/file1" ] && echo yes || echo no
yes
[root@node1 mnt]# [ -S "/mnt/file" ] && echo yes || echo no
no
[root@node1 mnt]# [ -e "/mnt/file" ] && echo yes || echo no
yes
[root@node1 mnt]# [ -c "/dev/vda" ] && echo yes || echo no
no
[root@node1 mnt]# [ -b "/dev/vda" ] && echo yes || echo no
yes
- 检测
编写脚本完成以下条件
file_check.sh 在执行时
如果脚本后未指定检测文件报错“未指定检测文件,请指定”
如果脚本后指定文件不存在报错“此文件不存在”
当文件存在时请检测文件类型并显示到输出中
[root@node1 mnt]# vim file_check.sh
#!/bin/bash
[ -z "$1" ] && {
echo Error: Please input file following script !!
exit
}
[ -e "$1" ] || {
echo Error: not found $1 !!
exit
}
[ -d "$1" ] && {
echo $1 is 目录 !!
exit
}
[ -S "$1" ] && {
echo $1 is 套接字 !!
exit
}
[ -L "$1" ] && {
echo $1 is 软连接 !!
exit
}
[ -f "$1" ] && {
echo $1 is 普通文件 !!
exit
}
[ -b "$1" ] && {
echo $1 is 快设备 !!
exit
}
[ -c "$1" ] && {
echo $1 is 字符设备 !!
exit
}
[root@node1 mnt]# sh file_check.sh /mnt/westos
/mnt/westos is 普通文件 !!
[root@westoslinux mnt]# sh file_check.sh /mnt/
/mnt/ is 目录 !!