本文共 7340 字,大约阅读时间需要 24 分钟。
用法:diff [options] files|directorys输出信息:[num1,num2][a|c|d][num3,num4]num1,num2 ##第一个文件中的行a ##添加c ##更改d ##删除< ##第一个文件中的内容> ##第二个文件中的内容num3,num4 ##第二个文件中的行常用参数:-b ##忽略空格-B ##忽略空行-i ##忽略大小写-c ##显示文件所有内容并标示不同-r ##对比目录-u ##合并输出
[root@localhost mnt]# cat westoshello westos[root@localhost mnt]# cat westos1hello westos123[root@localhost mnt]# diff westos westos11a2> 123[root@localhost mnt]# vim westos1[root@localhost mnt]# diff westos westos11c1,2< hello westos---> hello westos> 123[root@localhost mnt]# diff -b westos westos11a2> 123[root@localhost mnt]# diff -B westos westos1[root@localhost mnt]# diff westos westos11c1< hello westos---> Hello westos[root@localhost mnt]# diff -i westos westos1[root@localhost mnt]# diff -c westos westos1*** westos 2020-12-14 16:24:43.062000000 +0800--- westos1 2020-12-14 16:43:06.580000000 +0800****************** 1 ****--- 1,3 ----+ hello westos+ 123[root@localhost mnt]# mkdir westosdir westosdir1[root@localhost mnt]# touch westosdir/westosfile[root@localhost mnt]# diff -r westosdir westosdir1Only in westosdir: westosfile[root@localhost mnt]# diff -u westos westos1 > westos.path[root@localhost mnt]# cat westos.path --- westos 2020-12-14 16:24:43.062000000 +0800+++ westos1 2020-12-14 16:43:06.580000000 +0800@@ -1 +1,3 @@+ hello westos+123
patch 原文件 补丁文件-b ##备份原文件dnf install patch.x86_64 -y[root@localhost mnt]# diff -u westos westos1 > westos.path[root@localhost mnt]# patch -b westos westos.pathpatching file westos[root@localhost mnt]# lswestos westos1 westosdir westosdir1 westos.orig westos.path[root@localhost mnt]# cat westos.orig #备份的原文件hello westos[root@localhost mnt]# cat westoshello westos123[root@localhost mnt]# cat westos1hello westos123
cut -d : ##指定:为分隔符-f ##指定显示的列 5第五列| 3,5 3和5列|3-5 3到5列|5- 第五列以后|-5 到第五列-c ##指定截取的字符(数字用法同-f)
[root@localhost mnt]# grep bash -v /etc/passwd | cut -d : -f 1bindaemonadmlpsyncshutdownhaltmail
sort-n ##纯数字排序-r ##倒叙-u ##去掉重复-o ##输出到指定文件-t ##指定分隔符-k ##指定排序的列
[root@localhost mnt]# sort -n westos1256889912266868[root@localhost mnt]# sort -nr westos6868261299886521[root@localhost mnt]# sort -nr westos -o westos1[root@localhost mnt]# cat westos16868261299886521[root@localhost mnt]# sort -u westos112226566889[root@localhost mnt]# sort -t : -k 2 westos6:12:122:28:265:52:60:688:680:80:80:91:9[root@localhost mnt]# sort -t : -k 1 westos0:680:80:80:91:92:122:22:65:56:18:268:68
uniq-c #合并重复并统计重复个数-d #显示重复的行-u #显示唯一的行
[root@localhost mnt]# ifconfig enp1s0 | head -n 2 | tail -n 1 | cut -d " " -f 101.1.1.88
[root@d mnt]# grep bash /etc/passwd | sort -t : -k 3 -nr | head -n 1 | cut -d : -f 1lee
[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 2172.25.254.12172.25.254.212172.25.254.13
tr 'a-z' 'A-Z' ##小写转大写tr 'A-Z' 'a-z' ##大写转小写[root@localhost mnt]# ifconfig enp1s0 | head -2 | tail -1 | cut -d " " -f 101.1.1.88[root@localhost mnt]# ifconfig enp1s0 | head -2 | tail -1 | cut -d " " -f 10 | tr '1' '*'*.*.*.88[root@localhost mnt]# lspasswd westos[root@localhost mnt]# ls | tr 'a-z' 'A-Z'PASSWDWESTOS
test = [] ##[] 就相当于test命令"test $a = $b" = [ "$a" = "$b" ]test数字对比=!=-eq ##等于-ne ##不等于-le ##小于等于-lt ##小于-ge ##大于等于-gt ##大于test的条件关系-a ##并且-o ##或者test对空的判定-n ##nozero 判定内容不为空-z ##zero 判定内容为空test对于文件的判定-ef ##文件节点号是否一致(硬链)-nt ##文件1是不是比文件2新-ot ##文件1是不是比文件2老-d ##目录-S ##套结字-L ##软连接-e ##存在-f ##普通文件-b ##快设备-c ##字符设备
[root@node1 ~]# a=1[root@node1 ~]# b=1[root@node1 ~]# test "$a" = "$b" && echo yes || echo noyes[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 ##并且[root@node1 ~]# [ "$a" -le "0" -o "$a" -ge "1" ] && echo yes || echo no ##或者[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 判定内容为空yes[root@node1 ~]# [ -n "$c" ] && echo yes || echo no ##nozero 判定内容不为空no
[root@node1 mnt]# [ "/mnt/westos" -ef "/mnt/westos1" ] && echo yes || echo nono[root@node1 mnt]# [ "/mnt/westos" -nt "/mnt/westos1" ] && echo yes || echo noyes[root@node1 mnt]# [ "/mnt/westos" -ot "/mnt/westos1" ] && echo yes || echo nono[root@node1 mnt]# [ -d "/mnt/" ] && echo yes || echo noyes[root@node1 mnt]# ln -s /mnt/westos file1[root@node1 mnt]# ln /mnt/westos /mnt/file[root@node1 mnt]# [ -L "/mnt/file1" ] && echo yes || echo noyes[root@node1 mnt]# [ -S "/mnt/file" ] && echo yes || echo nono[root@node1 mnt]# [ -e "/mnt/file" ] && echo yes || echo noyes[root@node1 mnt]# [ -c "/dev/vda" ] && echo yes || echo nono[root@node1 mnt]# [ -b "/dev/vda" ] && echo yes || echo noyes
[root@d mnt]# vim usercheck.sh 1 #!/bin/bash 2 [ -z "$1" ] && { 3 echo "error:please input username following script!" 4 exit 5 } 6 id $1 &> /dev/null || { 7 echo "user $1 is not exist!" #当不能扔进垃圾箱时表示没有该用户 8 exit 9 } 10 11 USER_UID=$( id -u $1 ) 12 USER_SHELL=$( grep $1 /etc/passwd | cut -d : -f 7 ) 13 [ "$USER_UID" -eq "0" ] && { 14 echo $1 is supper user 15 exit 16 } 17 [ "$USER_UID" -lt "1000" ] && [ "$USER_SHELL" != "/bin/bash" ] && { 18 echo $1 is system user 19 exit 20 } 21 [ "$USER_UID" -ge "1000" ] && [ "$USER_SHELL" = "/bin/bash" ] && { 22 echo $1 is common user 23 exit 24 } 25 echo "unknow user type" 测试:[root@d mnt]# sh usercheck.sh westoswestos is common user[root@d mnt]# sh usercheck.sh linuxuser linux is not exist![root@d mnt]# sh usercheck.sh leelee is common user[root@d mnt]# sh usercheck.sh nginxnginx is system user[root@d mnt]# sh usercheck.sh rootroot is supper user
[root@d mnt]# vim filecheck.sh 1 #!/bin/bash 2 [ -z "$1" ] && { 3 echo error: please input file following script! 4 exit 5 } 6 ls -ld $1 > /dev/null || [ -e "$1" ] || { 7 echo error:not found $1 8 exit 9 } 10 [ -d "$1" ] && { 11 echo $1 is 目录 12 exit 13 } 14 [ -S "$1" ] && { 15 echo $1 is 套接字 16 exit 17 } 18 [ -L "$1" ] && { 19 echo $1 is 软连接 20 exit 21 } 22 [ -f "$1" ] && { 23 echo $1 is 普通文件 24 exit 25 } 26 [ -b "$1" ] && { 27 echo $1 is 快设备 28 exit 29 } 30 [ -c "$1" ] && { 31 echo $1 is 字符设备 32 exit 33 }测试:[root@node1 mnt]# sh file_check.sh /mnt/westos/mnt/westos is 普通文件 [root@westoslinux mnt]# sh file_check.sh /mnt//mnt/ is 目录
&& 符合条件作动作|| 不符合条件作动作
链接: .
转载地址:http://dnhzi.baihongyu.com/