(Turn) various brackets (large, medium, small) in the linux shell

1. Parentheses, parentheses ()

  1. Single parentheses ()

    ①Command group. Commands in parentheses will be executed sequentially in a new subshell, so variables in parentheses cannot be used by the rest of the script. Multiple commands in parentheses are separated by semicolons. The last command can be without a semicolon, and there is no need to have spaces between each command and the parentheses.
    ②Command substitution. Equivalent to `cmd`, the shell scans the command line once and finds the $(cmd) structure, executes the cmd in $(cmd) once, gets its standard output, and puts this output into the original command. Some shells do not support it, such as tcsh.
    ③ used to initialize the array. Such as: array=(abcd)
 
 

  2. Double parentheses (( ))

    ①Integer expansion. This extended calculation is an integer calculation and does not support floating point. The ((exp)) construct expands and evaluates an arithmetic expression, returning an exit status code of 1 if the result of the expression is 0, or "false", and a non-zero value returned by an expression The exit status code will be 0, or "true". If it is a logical judgment, the expression exp is 1 if it is true, and 0 if it is false.
    ②As long as the operators and expressions in parentheses conform to the C language operation rules, they can be used in $((exp)), even ternary operators. When performing operations with different carry (such as binary, octal, and hexadecimal), the output results are all automatically converted into decimal. For example: echo $((16#5f)) The result is 95 (hexadecimal to decimal)
    ③ Simply use (( )) to redefine variable values, such as a=5; ((a++)) can redefine $a to 6
    ④ Variables in double brackets can be prefixed without the $ sign. Multiple expressions within parentheses are supported separated by commas.
[python]  view plain copy  
 
  1. if ($i<5)  
  2. if [ $i -lt 5 ]  
  3. if  [$ a -ne  1  -a $ a! =  2  ]  
  4. if [ $a -ne 1] && [ $a != 2 ]  
  5. if [[ $a != 1 && $a != 2 ]]  
  6.    
  7. for i in $(seq 04);do echo $i;done   
  8. for i in `seq 04`;do echo $i;done   
  9. for ((i=0;i<5;i++));do echo $i;done  
  10. for i in {0..4};do echo $i;done  

b) Square brackets, square brackets []

  1. Single brackets []

    ①bash 的内部命令,[和test是等同的。如果我们不用绝对路径指明,通常我们用的都是bash自带的命令。if/test结构中的左中括号是调用test的命令标识,右中括号是关闭条件判断的。这个命令把它的参数作为比较表达式或者作为文件测试,并且根据比较的结果来返回一个退出状态码。if/test结构中并不是必须右中括号,但是新版的Bash中要求必须这样。
    ②Test和[]中可用的比较运算符只有==和!=,两者都是用于字符串比较的,不可用于整数比较,整数比较只能使用-eq,-gt这种形式。无论是字符串比较还是整数比较都不支持大于号小于号。如果实在想用,对于字符串比较可以使用转义形式,如果比较"ab"和"bc":[ ab \< bc ],结果为真,也就是返回状态为0。[ ]中的逻辑与和逻辑或使用-a 和-o 表示。
    ③字符范围。用作正则表达式的一部分,描述一个匹配的字符范围。作为test用途的中括号内不能使用正则。
    ④在一个array 结构的上下文中,中括号用来引用数组中每个元素的编号。
 
 

  2、双中括号[[ ]]

    ①[[是 bash 程序语言的关键字。并不是一个命令,[[ ]] 结构比[ ]结构更加通用。在[[和]]之间所有的字符都不会发生文件名扩展或者单词分割,但是会发生参数扩展和命令替换。
    ②支持字符串的模式匹配,使用=~操作符时甚至支持shell的正则表达式。字符串比较时可以把右边的作为一个模式,而不仅仅是一个字符串,比如[[ hello == hell? ]],结果为真。[[ ]] 中匹配字符串或通配符,不需要引号。
    ③使用[[ ... ]]条件判断结构,而不是[ ... ],能够防止脚本中的许多逻辑错误。比如,&&、||、<和> 操作符能够正常存在于[[ ]]条件判断结构中,但是如果出现在[ ]结构中的话,会报错。
    ④bash把双中括号中的表达式看作一个单独的元素,并返回一个退出状态码。

三)大括号、花括号 {}

  1、常规用法。

    ①大括号拓展。(通配(globbing))将对大括号中的文件名做扩展。在大括号中,不允许有空白,除非这个空白被引用或转义。第一种:对大括号中的以逗号分割的文件列表进行拓展。如 touch {a,b}.txt 结果为a.txt b.txt。第二种:对大括号中以点点(..)分割的顺序文件列表起拓展作用,如:touch {a..d}.txt 结果为a.txt b.txt c.txt d.txt
[python]  view plain  copy
 
  1. bogon:/home/bash # ls {ex1,ex2}.sh  
  2. ex1.sh  ex2.sh  
  3. bogon:/home/bash # ls {ex{1..3},ex4}.sh  
  4. ex1.sh  ex2.sh  ex3.sh  ex4.sh  
  5. bogon:/home/bash # ls {ex[1-3],ex4}.sh  
  6. ex1.sh  ex2.sh  ex3.sh  ex4.sh  
    ②代码块,又被称为内部组,这个结构事实上创建了一个匿名函数 。与小括号中的命令不同,大括号内的命令不会新开一个子shell运行,即脚本余下部分仍可使用括号内变量。括号内的命令间用分号隔开,最后一个也必须有分号。{}的第一个命令和左括号之间必须要有一个空格。
 
 

   2)几种特殊的替换结构:${var:-string},${var:+string},${var:=string},${var:?string}

      A,${var:-string}和${var:=string}:若变量var为空,则用在命令行中用string来替换${var:-string},否则变量var不为空时,则用变量var的值来替换${var:-string};对于${var:=string}的替换规则和${var:-string}是一样的,所不同之处是${var:=string}若var为空时,用string替换${var:=string}的同时,把string赋给变量var: ${var:=string}很常用的一种用法是,判断某个变量是否赋值,没有的话则给它赋上一个默认值。
      B. ${var:+string}的替换规则和上面的相反,即只有当var不是空的时候才替换成string,若var为空时则不替换或者说是替换成变量 var的值,即空值。(因为变量var此时为空,所以这两种说法是等价的) 
      C,${var:?string}替换规则为:若变量var不为空,则用变量var的值来替换${var:?string};若变量var为空,则把string输出到标准错误中,并从脚本中退出。我们可利用此特性来检查是否设置了变量的值。
      补充扩展:在上面这五种替换结构中string不一定是常值的,可用另外一个变量的值或是一种命令的输出。

  3)四种模式匹配替换结构${var%pattern},${var%%pattern},${var#pattern},${var##pattern}

     第一种模式:${variable%pattern},这种模式时,shell在variable中查找,看它是否一给的模式pattern结尾,如果是,就从命令行把variable中的内容去掉右边最短的匹配模式
     第二种模式: ${variable%%pattern},这种模式时,shell在variable中查找,看它是否一给的模式pattern结尾,如果是,就从命令行把variable中的内容去掉右边最长的匹配模式
     第三种模式:${variable#pattern} 这种模式时,shell在variable中查找,看它是否一给的模式pattern开始,如果是,就从命令行把variable中的内容去掉左边最短的匹配模式
     第四种模式: ${variable##pattern} 这种模式时,shell在variable中查找,看它是否一给的模式pattern结尾,如果是,就从命令行把variable中的内容去掉右边最长的匹配模式
     这四种模式中都不会改变variable的值,其中,只有在pattern中使用了*匹配符号时,%和%%,#和##才有区别。结构中的pattern支持通配符,*表示零个或多个任意字符,?表示零个或一个任意字符,[...]表示匹配中括号里面的字符,[!...]表示不匹配中括号里面的字符
[python]  view plain  copy
 
  1. bogon:/home/bash # var=testcase  
  2. bogon:/home/bash # echo $var  
  3. testcase  
  4. bogon:/home/bash # echo ${var%s*e}  
  5. test ca  
  6. bogon:/home/bash # echo $var  
  7. testcase  
  8. bogon:/home/bash # echo ${var%%s*e}  
  9. at  
  10. bogon:/home/bash # echo ${var#?e}  
  11. stcase  
  12. bogon:/home/bash # echo ${var##?e}  
  13. stcase  
  14. bogon:/home/bash # echo ${var##*e}  
  15.   
  16. bogon:/home/bash # echo ${var##*s}  
  17. e  
  18. bogon:/home/bash # echo ${var##test}  
  19. case  

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326634314&siteId=291194637