shell中的常用基础命令

1. diff

  • 用法:
    diff  [options] files | directorys
  • 输出信息:
    [num1,num2]           [a|c|d]              [num3,num4]
  • 字符含义
    num1,num2第一个文件中的行
    a添加
    c更改
    d删除
    <第一个文件中的内容
    >第二个文件中的内容
    num3,num4第二个文件中的行

 实验前提:

[root@westos_student50 mnt]# cat westos

hello westos

[root@westos_student50 mnt]# cat westos.new

hello westos

123

[root@westos_student50 mnt]# diff westos westos.new

1a2 第一个文件添加第二个文件的第二行123就与第二个文件一致

> 123

[root@westos_student50 mnt]# diff westos.new westos

2d1 第一个文件中如果删掉第二行123就与第二个文件一致

< 123

参数含义
-b忽略空格
-B忽略空行
-i忽略大小写
-c显示文件所有内容并标示不同
-r对比目录
-u合并输出

[root@westos_student50 mnt]# diff -b westos westos.new 加-b忽略文件中的空格

 

[root@westos_student50 mnt]# diff -B  westos westos.new 加-B忽略空行 

 [root@westos_student50 mnt]# diff -i westos westos.new 加-i忽略大小写

[root@westos_student50 mnt]# diff -c westos westos.new显示整个内容并用!标识不同的行

 [root@westos_student50 mnt]# diff -r test test1加-r对比两个目录的不同

 [root@westos_student50 mnt]# diff -u westos westos.new  加-u表示合并输出(多用于生成补丁

2.patch用法: patch + 原文件 + 补丁文件

[root@westos_student50 mnt]# diff -u westos westos.new > westos.path  生成补丁

[root@westos_student50 mnt]# dnf install patch -y

[root@westos_student50 mnt]# patch westos westos.path给westos文件打补丁生成补丁文件westos.path

[root@westos_student50 mnt]# patch -b westos westos.path在打补丁的同时备份源文件 3.cut 

参数含义
-d :指定 : 为分割符
-f指定显示的列 ,5表示第五列;3,5 表示3和5列;3-5 表示 3到5列
-c

指定截取的字符(数字用法同-f)

5-表示第五列以后                                          -5 表示到第五列

[root@westos_student50 mnt]# cp /etc/passwd .

[root@westos_student50 mnt]# vim passwd删掉多余行

[root@westos_student50 mnt]# cut -d : -f 1 passwd -d : 表示指定分割符为:-f表示产看第几列

[root@westos_student50 mnt]# cut -d : -f 1,7 passwd查看以:为分隔符的第一列和第七列 [root@westos_student50 mnt]# cut -d : -f 1-3 passwd查看以:为分隔符的第一列到第三列

[root@westos_student50 mnt]# cut -d : -f 3- passwd查看以:为分隔符的第三列到最后一列 

[root@westos_student50 mnt]# cut -c 1-4 passwd查看第一个到第四个字符

[root@westos_student50 mnt]# cut -c 1,4 passwd 查看第一个和第四个字符

4. sort

参数含义
-n纯数字排序
-r倒叙
-u去掉重复
-o输出到指定文件
-t指定分割符
-k指定排序的列

[root@westos_student50 mnt]# vim westos

[root@westos_student50 mnt]# sort westos把每一行第一个数字进行排序

[root@westos_student50 mnt]# sort -n  westos  按照数字大小排序 

[root@westos_student50 mnt]# sort -nr  westos 按数字大小倒叙 [root@westos_student50 mnt]# sort -nru  westos 去掉重复的进行倒叙 [root@westos_student50 mnt]# sort -nru  westos -o file 去掉重复的进行倒叙然后输出到file [root@westos_student50 mnt]# vim westos在文件中添加分割符: 

[root@westos_student50 mnt]# sort westos默认以第一列排序

[root@westos_student50 mnt]# sort -t : -k 2 -n  westos 加-t : 表示指定分割符: -k 2表示排序第二列 -n表示数字大小排序

 5. uniq

参数含义
-c合并重复并统计重复个数
-d显示重复的行
-u显示唯一的行

[root@westos_student50 mnt]# vim westos

[root@westos_student50 mnt]# sort -n westos | uniq -c按数字大小排序,合并重复数字并统计重复个数

[root@westos_student50 mnt]# sort -n westos | uniq -u 按数字大小排序并只显示唯一的数字

[root@westos_student50 mnt]# sort -n westos | uniq -d 按数字大小排序并只显示重复的数字

 [root@westos_student50 mnt]# rev file倒叙文件内容

4 3 2 1

[root@westos_student50 mnt]# tac file正序文件内容

1 2 3 4

6.tr(只能替换单个字符)

命令含义
tr ‘a-z’ ‘A-Z’小写转大写
tr ‘A-Z’ ‘a-z’大写转小写

[root@westos_student50 mnt]# tr 'l' 'w' < westos将westos中的l全部替换为w

[root@westos_student50 mnt]# tr 'a-z' 'A-Z' < westos将小写字母替换为大写字母

7.test

test = []                [] 就相当于test命令                       "test $a = $b" = [ "$a" = "$b" ]

test 数字对比

字符含义
取反
!=不等于
-eq等于
-ne不等于
-le小于等于
-lt小于
-ge大于等于
-gt大于

[root@westos_student50 mnt]# a=1

[root@westos_student50 mnt]# b=1 

[root@westos_student50 mnt]# c=2设定a=1 b=1 c=2

[root@westos_student50 mnt]# test "$a" = "$b" && echo yes || echo no判断a是否等于b ,等于输出yes 不等于输出no

[root@westos_student50 mnt]# [ "$a" = "$b" ] && echo yes || echo no判断a是否等于b ,等于输出yes 不等于输出no

[root@westos_student50 mnt]# [ "$a" != "$b" ] && echo yes || echo no判断a是否等于b ,正确输出yes 不正确输出no

[root@westos_student50 mnt]# [ "$a" -eq "$b" ] && echo yes || echo no判断a是否等于b ,等于输出yes 不等于输出no       (-eq表示等于)

[root@westos_student50 mnt]# [ "$a" -ne "$b" ] && echo yes || echo no判断a是否等于b ,正确输出yes 不正确输出no      (-ne表示不等于)

[root@westos_student50 mnt]# [ "$a" -ge "$b" ] && echo yes || echo no判断a是否大于等于b ,正确输出yes 不正确输出no (-ge表示大于等于)

[root@westos_student50 mnt]# [ "$a" -gt "$b" ] && echo yes || echo no判断a是否大于b ,正确输出yes 不正确输出no (-gt表示大于)

[root@westos_student50 mnt]# [ "$a" -lt "$b" ] && echo yes || echo no判断a是否小于b ,正确输出yes 不正确输出no (-lt表示小于)

[root@westos_student50 mnt]# [ "$a" -le "$b" ] && echo yes || echo no判断a是小于等于b ,正确输出yes 不正确输出no (-le表示小于等于)

test 的条件关系

参数含义
-a并且
-o或者

[root@westos_student50 mnt]# a=20

[root@westos_student50 mnt]# [ "$a" -gt "0" -a "$a" -lt "10" ] && echo yes || echo no 判断a是否大于0并且小于10 正确输出yes 不正确输出no

[root@westos_student50 mnt]# [ "$a" -lt "0" -o "$a" -gt "10" ] && echo yes || echo no判断是否a小于0或者大于10 正确输出yes 不正确输出no

[root@westos_student50 mnt]# [ ! "$a" -gt "0" -a "$a" -lt "10" ] && echo yes || echo no 反选判断a是否大于0并且小于10 正确输出yes 不正确输出no

test对空的判定

参数含义
-nnozero 判定内容不为空
-zzero 判定内容为空

[root@westos_student50 mnt]# [ -z "$d" ] && echo yes || echo no判断d是否为空,正确输出yes 不正确输出no

[root@westos_student50 mnt]# [ -n "$d" ] && echo yes || echo no判断d是否不为空,正确输出yes 不正确输出no

test 对于文件的判定

参数含义
-ef文件节点号是否一致(硬链)
-nt文件1是不是比文件2新
-ot文件1是不是比文件2老
-d目录
-S套结字
-L软链接
-e存在
-f普通文件
-b块设备
-c字符设备

[root@westos_student50 mnt]# [ "/mnt/test.sh" -nt "westos" ] && echo yes || echo no判断test.sh是否比westos新,正确输出yes 不正确输出no

[root@westos_student50 mnt]# [ "/mnt/test.sh" -ot "westos" ] && echo yes || echo no判断test.sh是否比westos老,正确输出yes 不正确输出no

[root@westos_student50 mnt]# ln test.sh test1.sh

[root@westos_student50 mnt]# [ "/mnt/test.sh" -ef "test1.sh" ] && echo yes || echo no判断test.sh和test1.sh节点号是否一致,正确输出yes 不正确输出no

[root@westos_student50 mnt]# [ -e "/mnt/haha"  ] && echo yes || echo no判断/mnt/haha是否存在,正确输出yes 不正确输出no

[root@westos_student50 mnt]# [ -f "/mnt/test.sh"  ] && echo yes || echo no判断/mnt/test.sh是不是普通文件,正确输出yes 不正确输出no

 8.&& ||

&& 符合条件作动作

|| 不符合条件作动作

[root@westos_student50 mnt]# ls /mnt/haha &> /dev/null && echo yes ||echo no 查看/mnt将结果扔到回收站,命令在执行的时候没有任何报错输出yes,命令在执行的时候有报错输出no

[root@westos_student50 mnt]# ls /mnt && echo yes ||echo no 浏览/mnt,符合输出结果和yes,不符合输出no

[root@westos_student50 mnt]# vim test.sh(一次使用多条命令)

[root@westos_student50 mnt]# sh test.sh

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

黑 哲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值