shell (1)
#####1.diff##########################
用法:
diff [options] files | directorys
输出信息:
[num1,num2] [a|c|d] [num3,num4]
num1,num2 ##第一个文件中的行
a ##添加
c ##更改
d ##删除
< ##第一个文件中的内容
> ##第二个文件中的内容
num3,num4 ##第二个文件中的行
常用参数:
-b ##忽略空格
-B ##忽略空行
-i ##忽略大小写
-c ##显示文件所有内容并表示不同
-r ##对比目录
-u ##合并输出
[[email protected] mnt]# vim westos.new ##给第一行后加空格
[[email protected] mnt]# diff westos westos.new
[[email protected] mnt]# diff -b westos westos.new
[[email protected] mnt]# vim westos.new
[[email protected] mnt]# diff -B westos westos.new
[[email protected] mnt]# diff -c westos westos.new
[[email protected] mnt]# diff -i westos westos.new
[[email protected] mnt]# diff -r westosdir1/ westosdir2/
[[email protected] mnt]# diff -u westos westos.new
#####2.patch######################
[[email protected] mnt]# yum install patch -y
patch ##原文件 补丁文件
[[email protected] mnt]# diff -u westos westos.new > westos.path
[[email protected] mnt]# cat westos
[[email protected] mnt]# cat westos.new
[[email protected] mnt]# diff -u westos westos.new > westos.path
[[email protected] mnt]# patch westos westos.path
[[email protected] mnt]# cat westos
-b ##备份原文件
[[email protected] mnt]# vim westos
[[email protected] mnt]# patch -b westos westos.path
#####3.cut#####################
[[email protected] mnt]# ls
[[email protected] mnt]# cp /etc/passwd .
cut
-d : ##指定 : 为分隔符
-f ##指定显示的列 5第五列 3,5 3和5列 3-5 3到5列 5- 第五列之后 -5 到第五列
-c ##指定截取的字符 (数字用法同-f)
[[email protected] mnt]# cut -d : -f 1 passwd
[[email protected] mnt]# cut -d : -f 1,7 passwd
[[email protected] mnt]# cut -d : -f 1-4 passwd
[[email protected] mnt]# cut -d : -f 3- passwd
[[email protected] mnt]# cut -d : -f -3 passwd
[[email protected] mnt]# cut -d : -f 2-3 passwd
[[email protected] mnt]# cut -c 1,4 passwd
[[email protected] mnt]# cut -c 1-4 passwd
#####4.sort####################
[[email protected] mnt]# vim westos
-n ##纯数字排序
-r ##倒叙
-u ##去掉重复
-o ##输出到指定文件
-t ##指定分隔符
-k ##指定排序的列
[[email protected] mnt]# sort -n westos
[[email protected] mnt]# sort -rn westos
[[email protected] mnt]# sort -rnu westos
[[email protected] mnt]# sort -rnu westos -o westos1
[[email protected] mnt]# vim westos
[[email protected] mnt]# sort -t : -k 2 westos -n
#####5.unip###############
[[email protected] mnt]# vim westos
-c ##合并重复并统计重复个数
-d ##显示重复的行
-u ##显示唯一的行
[[email protected] mnt]# sort -t : -k 2 westos -n | uniq -c
[[email protected] mnt]# sort -t : -k 2 westos -n | uniq -d
[[email protected] mnt]# sort -t : -k 2 westos -n | uniq -u
#####6.tr################
[[email protected] mnt]# vim westos
tr 'a-z' 'A-Z' ##小写转大写
tr 'A-Z' 'a-z' ##大写转小写
[ro[email protected] mnt]# cat westos | tr 'a-z' 'A-Z'
[[email protected] mnt]# cat westos | tr 'A-Z' 'a-z'
#####7.test######################
test = [ ] ##[ ] 就相当于test命令
"test $a = $b" = [ "$a" = "$b" ]
[[email protected] mnt]# test "$a" = "$b" && echo yes || echo no
[[email protected] mnt]# [ "$a" = "$b" ] && echo yes || echo no
test数字对比:
=
!=
-eq ##等于
-ne ##不等于
-le ##小于等于
-lt ##小于
-ge ##大于等于
-gt ##大于
[[email protected] mnt]# [ "$a" -eq "$b" ] && echo yes || echo no
[[email protected] mnt]# [ "$a" -ne "$b" ] && echo yes || echo no
[[email protected] mnt]# [ "$a" -le "$b" ] && echo yes || echo no
[[email protected] mnt]# [ "$a" -lt "$c" ] && echo yes || echo no
[[email protected] mnt]# [ "$a" -gt "$c" ] && echo yes || echo no
[[email protected] mnt]# [ "$a" -ge "$c" ] && echo yes || echo no
test条件关系:
-a ##并且
-o ##或者
[[email protected] mnt]# [ "$a" -gt "0" -a "$a" -le "10" ] && echo yes || echo no
[[email protected] mnt]# [ "$a" -lt "0" -o "$a" -gt "10" ] && echo no || echo yes
test对空的判断:
-n ##nozero 判断内容不为空
-z ##zero 判断内容为空
[[email protected] mnt]# [ -n "$a" ] && echo yes || echo no
[[email protected] mnt]# [ -z "$d" ] && echo yes || echo no
test对于文件的判定:
-ef ##文件节点号是否一致(硬链)
-nt ##文件1是不是比文件2新
-ot ##文件1是不是比文件2老
-d ##目录
-S ##套结字
-L ##软链接
-e ##存在
-f ##普通文件
-b ##快设备
-c ##字符设备
[[email protected] mnt]# [ "/mnt/file1" -nt "/mnt/file2" ] && echo yes || echo no
[[email protected] mnt]# [ "/mnt/file1" -ot "/mnt/file2" ] && echo yes || echo no
[[email protected] mnt]# [ "/mnt/westos" -ef "/mnt/westos1" ] && echo yes || echo no
[[email protected] mnt]# [ -d "/mnt/" ] && echo yes || echo no
[[email protected] mnt]# [ -e "/mnt/" ] && echo yes || echo no
[[email protected] mnt]# [ -f "/etc/passwd" ] && echo yes || echo no
[[email protected] mnt]# [ -L "/mnt/passwd" ] && echo yes || echo no
[[email protected] mnt]# [ -S "/var/lib/mysql/mysql.sock" ] && echo yes || echo no
[[email protected] mnt]# [ -b "/dev/nvme0n1" ] && echo yes || echo no
[[email protected] mnt]# [ -c "/dev/pts/0" ] && echo yes || echo no
#####8.&& || ########################
&& ##与 符合条件作动作
|| ##非 不符合条件作动作
[[email protected] mnt]# ping -c1 -w1 172.25.254.222 &> /dev/null && echo yes || echo no
[[email protected] mnt]# ping -c1 -w1 192.168.3.9 &> /dev/null && echo yes || echo no