• shell脚本变量操作


    使用#或%替换shell中的变量

    定义变量:

    pth=/abc/aab/abcabc/abcd/cb.txt

    变量替换(支持通配符但不支持正则表达式):

    ${var#pattern}:删除变量var中左边第一个能匹配到pattern左边的字符

    # echo ${pth#*bc}
    /aab/abcabc/abcd/cb.txt

    ${var##pattern}:删除变量var中左边最后一个能匹配到pattern左边的字符

    # echo ${pth##*abcd}
    /cb.txt

    ${var%pattern}:删除变量var中右边第一个能匹配到pattern右边的字符

    # echo ${pth%ab*}
    /abc/aab/abcabc/

    ${var%pattern}:删除变量var中右边第一个能匹配到pattern右边的字符

    # echo ${pth%%ab*}
    /

    测试通配符和正则表达式:

    pth=/abc/aab/abcabc1/abcd/cb.txt

    # echo ${pth##*abc[![:digit:]]}
    /cb.txt
    
    # echo ${pth##abc+*}
    /abc/aab/abcabc1/abcd/cb.txt

    记忆:键盘上#在%左边,所以#是去掉左边的字符,%是去掉右边的字符,两个总比一个长

    切片

    # echo ${pth}
    /abc/aab/abcabc/abcd/cb.txt

    ${var:start:[stop]}:从下标[start,stop)提取变量,下标从0开始,stop可以不给,表示取到结尾。注意是左闭右开的区间,

    # echo ${pth:0:4}
    /abc
    
    # echo ${pth:4:4}
    /aab
    
    # echo ${pth:4}
    /aab/abcabc/abcd/cb.txt
    
    # echo ${pth:4}
    /aab/abcabc/abcd/cb.txt

    变量替换

    ${var/pattern_string/target_string} :将匹配的第一个pattern_string变为target_string

    # echo ${pth/ab/}
    /c/a/cc/cd/cb.txt

    ${var/pattern_string/target_string} :将匹配的所有pattern_string变为target_string

    # echo ${pth//ab/}
    /c/a/cc/cd/cb.txt

    赋值变量

    ${var-target_string} :假如$var没有设置,使用target_string作为返回值。(空值和非空值不做处理)

    # path=/usr/bin/abc
    # echo ${path-/usr/local/bin/abc}
    /usr/bin/abc
    
    # path=
    # echo ${path-/usr/local/bin/abc}
    
    # unset path
    # echo ${path-/usr/local/bin/abc}
    /usr/local/bin/abc

    ${var:-target_string} :假如$var没有设置或者为空值,使用target_string 作为返回值。(非空值不做处理)

    # path=/usr/bin/abc
    
    # echo ${path:-/usr/local/bin/abc}
    /usr/bin/abc
    
    # path=
    
    # echo ${path:-/usr/local/bin/abc}
    /usr/local/bin/abc
    
    # unset path
    
    # echo ${path:-/usr/local/bin/abc}
    /usr/local/bin/abc

    ${var+target_string} :假如$var为非空值或者为空值,使用target_string 作为返回值。(没有设定时不做处理)

    # path=/usr/bin/abc
    # echo ${path+/usr/local/bin/abc}
    /usr/local/bin/abc
    
    # path=
    
    # echo ${path+/usr/local/bin/abc}
    /usr/local/bin/abc
    
    # unset path
    
    # echo ${path+/usr/local/bin/abc}

    ${var:-target_string} :假如$var为非空值,使用target_string 作为返回值。(空值和未设定时不做处理)

    # path=/usr/bin/abc
    
    # echo ${path:+/usr/local/bin/abc}
    /usr/local/bin/abc
    
    # path=
    
    # echo ${path:+/usr/local/bin/abc}
    
     
    # unset path
    
    # echo ${path:+/usr/local/bin/abc}

    ${var=target_string} :当var未设置时,使用target_string作为返回值,并且将target_string负值给var。(空值和非空值不做处理)

    # path=/usr/bin/abc
    # echo ${path=/usr/local/bin/abc}
    /usr/bin/abc
    
    # echo ${path=/usr/local/bin/abc}
     
    
    # unset path
    # echo ${path=/usr/local/bin/abc}
    /usr/local/bin/abc

    ${var:?target_string} :当var未设置或为空值时,将target_string输出至标准错误(非空值不做处理)

    # path=/usr/bin/abc
    
    # echo ${path:?/usr/local/bin/abc}
    /usr/bin/abc
    
    # path=
    
    # echo ${path:?/usr/local/bin/abc}
    -bash: path: /usr/local/bin/abc
    
    # unset path
    
    # echo ${path:?/usr/local/bin/abc}
    -bash: path: /usr/local/bin/abc

    ${var:?target_string} :如果var没有设定,将target_string输出至标准错误(空值和非空值不做处理)

    获取变量长度 ${#var}

    # path=/usr/bin/abc
    
    # echo ${#path}
    12

    数组操作

    定义数组

    # array=("abc" "bcd" "abccd" "acde")

    获取数组的所有元素 ${array[@]} 或者 ${array[*]}

    # echo ${array[@]}
    abc bcd abccd acde
    
    # echo ${array[*]}
    abc bcd abccd acde

    获取数组元素个数 ${#array}

    # echo ${#array}
    3

    获取数组中某个元素的长度 ${#array[下标]}

    # echo ${#array[2]}
    5

    给数组的某个元素赋值array[下标]=value

    # array[1]="def"
    
    # echo ${array[*]}
    abc def abccd acde

    删除数组中的元素 unset array[下标]

    # unset array[0]
    
    # echo ${array[*]}
    def abccd acde

    数组分片访问 ${array[@]:n:m} 从第n个元素开始,取m个元素

    # echo ${array[*]}
    def abccd acde
    
    # echo ${array[*]:1:2}
    def abccd

    替换数组元素内容

    # echo ${array[*]}
    def abccd acde
    
    # echo ${array[2]/c/f}        将第二个元素的第一个c替换成f
    abfcd
    
    # echo ${array[2]//c/f}       将第二个元素中所有的c替换成f
    abffd
  • 相关阅读:
    Linux Shell tr 命令详解
    Shell统计每个单词出现的个数
    启明4-29团队进度博客
    启明4-28团队进度博客
    启明4-27团队进度博客
    启明4-26团队进度博客
    启明4-25团队进度博客
    启明4-24团队博客
    每日总结8
    启明4-23团队进度博客
  • 原文地址:https://www.cnblogs.com/zh-dream/p/12392246.html
Copyright © 2020-2023  润新知