• 1、shell脚本中的命令详解


    1、diff命令的使用方法

    用法:

    diff [options] files|directorys

    输出信息:
    [num1,um2] [a|c|d] [num3,num4]

    num1,num2第一个文件中的行
    a添加
    c更改
    d删除
    <第一个文件中的内容
    >第二个文件中的内容
    num3,num4第二个文件中的行
    [root@server1 ~]# cd /mnt/
    [root@server1 mnt]# vim westos 
    hello westos 
    [root@server1 mnt]# vim westos.news
    hello westos
    123
    [root@server1 mnt]# diff westos westos.news 
    1a2    其中1表示第一个文件的行,2表示第二个文件的行,a表示添加
    > 123     >表示第二个文件中的内容
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    常用参数:

    -b##忽略空格
    -B##忽略空行
    -i##忽略大小写
    -c##显示文件所有内容并标示不同
    -r##对比目录
    -u##合并输出
    [root@server1 mnt]# vim westos 
    hello westos 
    [root@server1 mnt]# vim westos.news  
    hello  westos   添加一个空格
    [root@server1 mnt]# diff westos westos.news 
    1c1
    < hello westos
    ---
    > hello  westos
    [root@server1 mnt]# diff -b westos westos.news  -b 表示忽略,忽略空格后两个文件对比内容一样
    [root@server1 mnt]# vim westos.news 
    hello westos
                            加一行空行
    [root@server1 mnt]# diff westos westos.news 
    1a2
    >                   表示第二个文件的第二行有空格
    [root@server1 mnt]# diff -B westos westos.news   表示忽略空行
    [root@server1 mnt]# vim westos.news  
    hello Westos  修改为大写
    [root@server1 mnt]# diff westos westos.news 
    1c1
    < hello westos
    ---
    > hello Westos
    [root@server1 mnt]# diff -i westos westos.news   忽略大小写
    
    [root@server1 mnt]# vim westos.news 
    123
    hello Westos
    456
    [root@server1 mnt]# vim westos
    123
    hell Westos
    456
    [root@server1 mnt]# diff -c westos westos.news   -c表示显示文件所有内容,并标识不同
    *** westos	2022-09-02 11:16:32.631000000 +0800
    --- westos.news	2022-09-02 11:15:09.527000000 +0800
    ***************
    *** 1,3 ****
      123
    ! hell Westos
      456
    --- 1,3 ----
      123
    ! hello Westos
      456
    [root@server1 mnt]# mkdir westosdir westosdir1 
    [root@server1 mnt]# touch westosdir1/file 
    [root@server1 mnt]# diff -r westosdir westosdir1/  -r表示对比两个目录
    Only in westosdir1/: file
    [root@server1 mnt]# diff -u westos westos.news > westos.path   -u 表示合并输出,用于打补丁
    --- westos	2022-09-02 11:16:32.631000000 +0800
    +++ westos.news	2022-09-02 11:15:09.527000000 +0800
    @@ -1,3 +1,3 @@
     123
    -hell Westos
    +hello Westos
     456
    [root@server1 mnt]# yum install patch -y    打补丁需要安装补丁管理器
    [root@server1 mnt]# patch westos westos.path   在westos上打补丁
    patching file westos
    [root@server1 mnt]# cat westos  可以发现和westos.news内容一致
    123
    hello Westos
    456
    注意:打完补丁后原文件就没了,需要备份怎么办了?
    [root@server1 mnt]# diff -u westos westos.news > westos.path  修改过后的文件需要重新打补丁
    [root@server1 mnt]# patch -b westos westos.news    -b 表示打补丁后保存原文件
    [root@server1 mnt]# patch westos westos.path 
    patching file westos
    [root@server1 mnt]# ls
    westos  westosdir  westosdir1  westos.news  westos.orig  westos.path      westos.orig 为备份的原文件
    [root@server1 mnt]# cat westos  打补丁的文件也打上去了
    123
    hello Westos
    456
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76

    2. sort

    -n##纯数字排序
    -r倒叙
    -u去掉重复
    -o输出到指定文件
    -t指定分割符
    -k指定排序的列
    [root@server1 mnt]# cat westos 
    5
    9
    2
    4
    5
    1
    56
    9
    1
    0
    5
    4
    3
    8
    [root@server1 mnt]# sort westos  排序,默认是以第一行排序
    0
    0
    0
    0
    1
    1
    1
    1
    1
    13
    13
    [root@server1 mnt]# sort -n westos   纯数字排序
    0
    0
    1
    1
    1
    1
    2
    3
    4
    4
    [root@server1 mnt]# sort -rn westos   倒叙排序
    56
    56
    45
    13
    9
    9
    9
    8
    5
    5
    4
    4
    3
    [root@server1 mnt]# sort -n westos  -o testfile    -o表示将排序结果输出到指定文件
    [root@server1 mnt]# ls
    testfile  westos
    [root@server1 mnt]# sort -un westos   -u表示去掉重复项
    0
    1
    2
    3
    4
    5
    8
    9
    13
    45
    56
    [root@server1 mnt]# vim westos  
    a:1
    a:56
    a:9
    a:1
    a:0
    a:5
    a:4
    a:3
    3:8
    4:0
    5:13
    6:45
    3:9
    a:2
    a:4
    a:5
    a:1
    a:56
    6:9
    1:1
    [root@server1 mnt]# sort -n -t : -k 2 westos    -t表示指定分隔符   -k 2 表示指定第二列排序
    4:0
    a:0
    1:1
    a:1
    a:1
    a:1
    a:2
    a:3
    a:4
    a:4
    a:5
    a:5
    3:8
    3:9
    6:9
    a:9
    5:13
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106

    3. uniq (结合sort排序命令使用)

    -c合并重复并统计重复个数
    -d显示重复的行
    -u显示唯一的行
    [root@server1 mnt]# vim westos 
    1
    56
    9
    1
    0
    5
    4
    3
    8
    0
    13
    45
    9
    2
    4
    5
    1
    56
    9
    1
    [root@server1 mnt]# sort -n westos | uniq -c    uniq -c  表示合并重复的项,并显示重复个数
          2 0
          4 1
          1 2
          1 3
          2 4
          2 5
          1 8
          3 9
          1 13
          1 45
          2 56 
    [root@server1 mnt]# sort -n westos | uniq -d    显示重复的行
    0
    1
    4
    5
    9
    56
    [root@server1 mnt]# sort -n westos | uniq -u   显示唯一的行
    2
    3
    8
    13
    45
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46

    4. cut

    -d :指定 : 为分割符
    -f指定显示的列 ,5第五列 ,3,5 3和5列,3-5 3到5列, 5- 第五列以后 ,-5 到第五列
    -c指定截取的字符(数字用法同-f)
    [root@server1 mnt]# cp /etc/passwd  .
    [root@server1 mnt]# cut -d : -f 1 passwd     -d表示指定分隔符, -f 1 显示第一列
    root
    bin
    daemon
    adm
    lp
    sync
    shutdown
    halt
    mail
    operator
    games
    [root@server1 mnt]# cut -d : -f 1,2 passwd    -f 1,2显示第一列和第二列
    root:x
    bin:x
    daemon:x
    adm:x
    lp:x
    sync:x
    shutdown:x
    halt:x
    mail:x
    operator:x
    games:x
    [root@server1 mnt]# cut -d : -f 1-4 passwd   显示1到4列
    root:x:0:0
    bin:x:1:1
    daemon:x:2:2
    adm:x:3:4
    lp:x:4:7
    sync:x:5:0
    shutdown:x:6:0
    halt:x:7:0
    mail:x:8:12
    operator:x:11:0
    games:x:12:100
    [root@server1 mnt]# cut -d : -f 4- passwd   显示第4列到最后一列
    0:root:/root:/bin/bash
    1:bin:/bin:/sbin/nologin
    2:daemon:/sbin:/sbin/nologin
    4:adm:/var/adm:/sbin/nologin
    7:lp:/var/spool/lpd:/sbin/nologin
    0:sync:/sbin:/bin/sync
    0:shutdown:/sbin:/sbin/shutdown
    0:halt:/sbin:/sbin/halt
    12:mail:/var/spool/mail:/sbin/nologin
    0:operator:/root:/sbin/nologin
    100:games:/usr/games:/sbin/nologin
    [root@server1 mnt]# cut -d : -f -4 passwd   显示第一列到第4列
    root:x:0:0
    bin:x:1:1
    daemon:x:2:2
    adm:x:3:4
    lp:x:4:7
    sync:x:5:0
    shutdown:x:6:0
    halt:x:7:0
    mail:x:8:12
    operator:x:11:0
    games:x:12:100
    [root@server1 mnt]# cut -c 2 passwd   截取每行第2个字符
    o
    i
    a
    d
    p
    y
    h
    a
    a
    p
    a
    [root@server1 mnt]# cut -c 1-2 passwd   截取每行第1到第4个字符
    ro
    bi
    da
    ad
    lp
    sy
    sh
    ha
    ma
    op
    ga
    [root@server1 mnt]# cut -c 4- passwd   截取每行第4个字符到最后的字符
    t:x:0:0:root:/root:/bin/bash
    :x:1:1:bin:/bin:/sbin/nologin
    mon:x:2:2:daemon:/sbin:/sbin/nologin
    :x:3:4:adm:/var/adm:/sbin/nologin
    x:4:7:lp:/var/spool/lpd:/sbin/nologin
    c:x:5:0:sync:/sbin:/bin/sync
    tdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    t:x:7:0:halt:/sbin:/sbin/halt
    l:x:8:12:mail:/var/spool/mail:/sbin/nologin
    rator:x:11:0:operator:/root:/sbin/nologin
    es:x:12:100:games:/usr/games:/sbin/nologin
    [root@server1 mnt]# cut -c 1,4 passwd   截取每行第1和第4的字符
    rt
    b:
    dm
    a:
    lx
    sc
    st
    ht
    ml
    or
    ge
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109

    5. && ||

    &&符合条件动作
    II不符合条件动作
    [root@server1 mnt]# ping -c1 -w1 172.25.254.111 &>/dev/null && echo yes || echo no  表示能ping通显示yes,不能ping通表示no
    no
    [westos@server1 ~]$ find /etc/ -name passwd  &> /dev/null && echo yes || echo no   命令有报错显示no
    no
    [westos@server1 ~]$ find /etc/ -name passwd  &> /dev/null && (echo yes;echo 1) || (echo no;echo 2)  可以执行多条,用括号阔起来表示一个整体
    no
    2
    在脚本里书写方法:
    [root@server1 ~]# vim test.sh
    ping -c1 -w1 172.25.254.111 &> /dev/null && {    符合条件取123
               echo 1
               echo 2
               echo 3
    }||{                                 不符合条件取456
               echo 4
               echo 5
               echo 6
    } 
    [root@server1 ~]# sh test.sh   运行       
    4
    5
    6
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    6. tr

    tr ‘a-z’ ‘A-Z’小写转大写
    tr ‘A-Z’ ‘a-z’大写转小写
    [root@server1 mnt]# vim westos 
    hello westos 
    [root@server1 mnt]# tr 'a-z' 'A-Z' < westos
    HELLO WESTOS 
    [root@server1 mnt]# tr 'l' 'a' < westos    可以转换字符,只能转换一个,不能批量转换
    heaao westos 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    7、test

    7.1 test数字对比

    -eq等于
    -ne不等于
    -le小于等于
    -lt小于
    -ge大于等于
    -gt大于
    [root@server1 ~]# a=1
    [root@server1 ~]# b=1
    [root@server1 ~]# test "$a" = "$b"  && echo yes || echo no 
    yes
    [root@server1 ~]# [ "$a" = "$b" ] && echo yes || echo no    test也可以写成中括号
    yes
    [root@server1 ~]# [ "$a" -eq "$b" ] && echo yes || echo no   -eq也表示相等
    yes
    [root@server1 ~]# [ "$a" -ne "$b" ] && echo yes || echo no   -ne表示不等
    no
    [root@server1 ~]# [ "$a" -lt "$b" ] && echo yes || echo no    -lt表示小于
    no
    [root@server1 ~]# [ "$a" -le "$b" ] && echo yes || echo no  -le表示小于等于
    yes
    [root@server1 ~]# [ "$a" -gt "$b" ] && echo yes || echo no    -gt表示大于
    no
    [root@server1 ~]# [ "$a" -ge "$b" ] && echo yes || echo no    -ge表示大于等于
    yes
    [root@server1 ~]# [ ! "$a" -ge "$b" ] && echo yes || echo no    !表示条件如果不成立为真,反选
    no
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    7.2 test条件对比

    -a并且
    -o或者
    [root@server1 ~]# c=5  
    [root@server1 ~]# [ "$c" -gt "0" -a "$c" -lt "10" ] && echo yes || echo no   判定5是不是在10以内,-a 并且
    yes
    [root@server1 ~]# [ "$c" -le "0" -o "$c" -ge "10" ] && echo yes || echo no  判定5是不是在10以外  -o 或者
    no
    
    • 1
    • 2
    • 3
    • 4
    • 5

    7.3 对空的判定

    -nnozero 判定内容不为空
    -zzero 判定内容为空
    [root@server1 ~]# [ -z "$d" ] && echo yes || echo no   -z 判定内容为空
    yes
    [root@server1 ~]# [ -n "$d" ] && echo yes || echo no   -n 判定内容不为空
    no
    [root@server1 ~]# d=5
    [root@server1 ~]# [ -n "$d" ] && echo yes || echo no  d=5不为空,所以输出结果为yes
    yes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    7.4 test对于文件的判定

    -ef文件节点号是否一致(硬链)
    -nt文件1是不是比文件2新
    -ot文件1是不是比文件2老
    -d目录
    -S套结字
    -L软连接
    -e存在
    -f普通文件
    -b快设备
    -c字符设备
    [root@server1 mnt]# [ "/mnt/westos1" -nt "/mnt/westos" ] && echo yes || echo no    判定westos1是否比westos新
    yes
    [root@server1 mnt]# [ "/mnt/westos1" -ot "/mnt/westos" ] && echo yes || echo no   判定westos1是否比westos老
    no
    [root@server1 mnt]#  ln /mnt/westos1 /mnt/westos2   软连接
    [root@server1 mnt]# ls -i /mnt/
    33575112 westos  33575114 westos1  33575114 westos2
    [root@server1 mnt]# [ "/mnt/westos1" -ef "/mnt/westos2" ] && echo yes || echo no  判定westos1和westos2节点号(id)是否一致
    yes
    [root@server1 mnt]# [ "/mnt/westos1" -ef "/mnt/westos" ] && echo yes || echo no   
    no
    [root@server1 mnt]# [ -e "/mnt/westos" ] && echo yes || echo no    -e判定文件是否存在
    yes
    [root@server1 mnt]# [ -d "/mnt/westos" ] && echo yes || echo no   -d判定是不是目录
    no
    [root@server1 mnt]# [ -f "/mnt/westos" ] && echo yes || echo no   -f判定是不是文件
    yes
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    8、练习

    test :

    1. ifconfig 网卡,可以显示此网卡的信息
      显示信息中包含此网卡使用的ip 地址
    [root@server1 mnt]# ifconfig eth0 | head -n 2 | tail -n 1 | cut -d " " -f 10
    172.25.50.1
    [root@server1 mnt]# ifconfig  eth0 | awk '/inet\>/{print $2}'
    172.25.50.1
    
    • 1
    • 2
    • 3
    • 4

    2、如何判定用户存不存在

    [root@server1 mnt]# vim test.sh 
    #!/bin/bash 
    id $* &> /dev/null && {
         echo $* is exist
    }||{
         echo $* is not exist
    } 
    [root@server1 mnt]# sh test.sh hello
    hello is not exist
    [root@server1 mnt]# sh test.sh westos
    westos is exist 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    Halcon 模板匹配实战代码(一)
    java毕业生设计高校固定资产采购预算申报系统计算机源码+系统+mysql+调试部署+lw
    BTCs打造区块链加营销广告数字流量新形式
    车载以太网-传输层-UDP
    pip 安装任意软件包报错
    学成在线_课程查询_前端分页条不显示
    【矩阵论】1.准备知识(下)
    B024 – STM32温湿度控制体温检测烟雾报警系统
    电子科学与技术毕设题目选题推荐
    Spread for ASP.NET 15.2 个性化需求中文版
  • 原文地址:https://blog.csdn.net/qq_43114229/article/details/126636748