一、diff用法
编写两个有一行不同的文件westos westos1
[root@M1 mnt]# diff westos westos1 ##对比两个文件不同的地方
2c2
< hello westos
---
> hello westos1
[root@M1 mnt]# diff -b westos westos1 ##忽略文件中空格导致的不同
[root@M1 mnt]# diff -B westos westos1 ##忽略文件中空行导致的不同
[root@M1 mnt]# diff -i westos westos1 ##忽略文件中大小写导致的不同
[root@M1 mnt]# diff -c westos westos1 ##显示文件的全文对比并标记不同点
*** westos 2020-12-13 09:20:58.544000000 +0800
--- westos1 2020-12-13 09:29:28.973000000 +0800
***************
*** 1,2 ****
123
! hello westos
--- 1,2 ----
123
! Hello westos
[root@M1 mnt]# diff -u westos westos1 ##合并输出
--- westos 2020-12-13 09:20:58.544000000 +0800
+++ westos1 2020-12-13 09:29:28.973000000 +0800
@@ -1,2 +1,2 @@
123
-hello westos
+Hello westos
[root@M1 mnt]# diff -u westos westos1 > westos.path ##生成补丁文件
二、patch用法
[root@M1 mnt]# dnf install patch -y ##安装patch功能
[root@M1 mnt]# patch westos westos.path ##给westos文件打补丁但不保存原文件
[root@M1 mnt]# patch -b westos westos.path ##给westos文件打补丁并保存原文件
三、cut用法
[root@M1 mnt]# grep bash -v /etc/passwd > nologin ##查看passwd文件中所有不能登陆的用户并保存到nologin文件中
[root@M1 mnt]# cut -d : -f 1 nologin ##查看文件以 : 作为分隔符的第一列
[root@M1 mnt]# cut -d : -f -2 nologin ##查看文件以 : 作为分隔符到第二列的信息
[root@M1 mnt]# cut -d : -f 5- nologin ##第5列到最后一列的内容
[root@M1 mnt]# cut -c 4 nologin ##查看文件第4列字符
[root@M1 mnt]# cut -c -4 nologin ##查看文件前4列字符
四、sort 排序
[root@M1 mnt]# sort -n file ##正序排序
1
2
3
3
4
4
12
32
34
55
[root@M1 mnt]# sort -nr file ##倒序排序
55
34
32
12
4
4
3
3
2
1
[root@M1 mnt]# sort -u file ##去掉重复数
[root@M1 mnt]# sort -n file -o westos ##输出到指定文件
[root@M1 mnt]# sort -t : -k 2 file ##指定分隔符和排序的列
[root@M1 mnt]# uniq -d file ##显示重复的行
[root@M1 mnt]# uniq -c file ##合并重复并统计重复个数,先排序才能得出正确结果
[root@M1 mnt]# uniq -u file ##显示唯一的行
######命令测试########
1、ifconfig 网卡,可以显示此网卡信息,显示信息中包含此网卡使用的ip地址,请用命令过滤此ip并在输出时只显示ip,其他信息不显示。
[root@M1 mnt]# ifconfig enp1s0 | head -n 2 | tail -n 1 | cut -d " " -f 10
2、找出能登陆系统用户中UID最大的用户,并显示其名称。
[root@M1 mnt]# grep bash /etc/passwd | sort -t : -k 3 -n | tail -n 1 | cut -d : -f 1
3、当前主机为web服务器,请抓取访问web服务器次数排在前 5 的地址。
[root@westos_student5 logs]# cut -d " " -f 1 /etc/httpd/logs/access_log | sort -nr | uniq -c| sort -nr|head -5 | cut -c 9-
172.25.254.5
172.25.254.105
172.25.254.194
172.25.254.122
172.25.254.205
五、test命令
[root@M1 mnt]# a=1
[root@M1 mnt]# b=1
[root@M1 mnt]# test "$a" = "$b" && echo yes || echo no ##判断a,b是否相等 ‘&&’ 并、 ‘||’ 或
yes
[root@M1 mnt]# b=2
[root@M1 mnt]# test "$a" = "$b" && echo yes || echo no
no
[root@M1 mnt]# [ "$a" = "$b" ] && echo yes || echo no ##test=[] 都要使用空格隔开
no
[root@M1 mnt]# [ "$a" != "$b" ] && echo yes || echo no ##不等于
yes
[root@M1 mnt]# [ "$a" -eq "$b" ] && echo yes || echo no ##等于
no
[root@M1 mnt]# [ "$a" -ne "$b" ] && echo yes || echo no ##不等于
yes
[root@M1 mnt]# [ "$a" -le "$b" ] && echo yes || echo no ##小于等于
yes
[root@M1 mnt]# [ "$a" -lt "$b" ] && echo yes || echo no ##小于
yes
[root@M1 mnt]# [ "$a" -ge "$b" ] && echo yes || echo no ##大于等于
no
[root@M1 mnt]# [ "$a" -gt "$b" ] && echo yes || echo no ##大于
no
[root@M1 mnt]# [ "$a" -gt "0" -a "$b" -lt "10" ] && echo yes || echo no ## -a 并且
yes
[root@M1 mnt]# [ "$a" -gt "0" -o "$b" -lt "10" ] && echo yes || echo no ## -o 或者
yes
[root@M1 mnt]# [ "$a" -gt "0" ] && [ "$b" -lt "10" ] && echo yes || echo no ##并且
yes
[root@M1 mnt]# [ "$a" -gt "0" ] || [ "$b" -lt "10" ] && echo yes || echo no ##或者
yes
[root@M1 mnt]# [ -z "$b" ] && echo yes || echo no ## zero 判定内容为空
no
[root@M1 mnt]# [ -n "$b" ] && echo yes || echo no ## nozero 判定内容不为空
yes
六、test 对于文件的判定
[root@M1 mnt]# [ -d "/mnt" ] && echo yes || echo no
用法同上
-ef ##文件节点号是否一致(硬链接)
-nt ##文件1是否比文件2新(创建时间早)
-ot ##文件1是否比文件2旧(创建时间晚)
-d ##是否为目录
-S ##是否为套接字
-L ##是否为软链接
-e ##是否存在
-f ##是否为普通文件
-b ##是否为块设备(/dev/vda)
-c ##是否为字符设备(/dev/pts/0)
附:检测文件类别的脚本
#!/bin/bash
[ -z "$1" ] && {
echo "未指定检测文件,请指定!"
exit
}
[ ! -e "$1" ] && {
echo "此文件不存在!"
exit
}
[ -d "$1" ] && {
echo "目录"
exit
}
[ -S "$1" ] && {
echo "套接字"
exit
}
[ -L "$1" ] && {
echo "软链接"
exit
}
[ -f "$1" ] && {
echo "普通文件"
exit
}
[ -b "$1" ] && {
echo "块设备"
exit
}
[ -c "$1" ] && {
echo "字符设备"
exit
}
检测用户类型的脚本
#!/bin/bash
[ -z "$1" ] && {
echo "ERROR!"
exit
}
id $1 &> /dev/null || {
echo "user $1 is not exist!"
exit
}
user_id=$( id -u $1 )
user_shell=$( grep $1 /etc/passwd | cut -d : -f 7)
[ "$user_id" = "0" ] && {
echo $1 is supper user!
exit
}
[ "$user_id" -lt "1000" ] && [ "$user_shell" != "/bin/bash" ] && {
echo $1 is systemuser!
exit
}
[ "$user_id" -gt "1000" ] && [ "$user_shell" = "/bin/bash" ] && {
echo $1 is common user!
exit
}
echo "unknow user type!"
脚本中 $1 可以, $c 等字母不行
因为字母有其他的含义,例如$c是更改最后一行。