In Linux Shell What is the role of the various brackets?

Xiao Bian to share with you today to talk about what is the role of various articles parentheses in Linux Shell in? For just Linux entry under the junior partner must study hard, familiar with Linux small partners know that with each shell used in kinds of brackets, then the use of these brackets you know what? Let's take a look at it!

 

A small brackets, parentheses ()

1, a single parentheses ()

① command group. Brackets command will open a new sub-order execution shell, so parentheses variables can not be part of the rest of the script to use. Multiple commands brackets separated by a semicolon, the last command can no semicolon, do not have a space between the command and the bracket.

② command substitution. Equivalent to `cmd`, shell command line scanning again found cmd execution $ (cmd) structure, put $ (cmd) in the first, to obtain its standard output, then this output into the original order. Some shell does not support, such as tcsh.

③ is used to initialize an array. Such as: array = (abcd)

2, double parentheses (())

① integer extension. This expansion calculations are integer calculations do not support floating-point type. (() Exp) structure of the extended arithmetic expression and a calculated value, if the expression evaluates to 0, then returns an exit status code 1, or "false", and the expression of a non-zero value returned exit status will be zero, or "true". If the logic, the expression exp is true, or false or 0.

② as long as the brackets operators, expressions conform to the C language rules of operation are available in $ ((exp)), the even ternary operator. When differently carry (such as binary, octal, hexadecimal) operation, the output of all automatically converted into decimal. Such as: echo $ ((16 # 5f)) found to be 95 (hexadecimal turn decimal)

③ simply using (()) may be redefined variable value, such as a = 5; ((a ++)) may be redefined to 6 $ a

④ commonly used in the arithmetic comparison, double parentheses may not be used in the variable $ sign prefix. Support brackets more expressions separated by commas. As long as the expression in parentheses in line with the C language operation rule, such as may be directly used for ((i = 0; i <5; i ++)), without using double parentheses, for the for i in `seq 0 4` or for i in {0..4}. Another example can be used directly if (($ i <5)), without using double parentheses, compared if [$ i -lt 5].

Second, the brackets, the square brackets []

1, a single square brackets []

①bash internal commands, [and test are equivalent. If we do not specify an absolute path, usually we use all bash built-in command. if / test structure left bracket command calling identification test, right bracket is closed to determine the conditions. This command its arguments as comparison expressions or file as a test, and to return an exit status based on results of the comparison. if / test structure is not necessarily right bracket, but the new version of Bash required to be so.

②Test and [] are only available in comparison operator == and! =, Both of which are used for string comparisons, for comparison is not an integer, an integer comparison using only -eq, -gt this form. Whether the comparison string or integer comparison does not support greater-than sign less than sign. If you really want to use, can be used for comparing strings form of escape, if comparing "ab" and "bc": [ab \ <bc], the result is true, that is, the return status is 0. [] And logic with logic or -a -o and FIG.

③ range of characters. As part of a regular expression, to describe a range of characters matching. Can not be used as a regular test purposes within the brackets.

④ In the context of an array structure, the brackets used to reference number of each element in the array.

2, double brackets [[]]

① [[keyword bash programming language. Not a command, [[]] structure] structure is more versatile than [. All characters will not happen filename extension or word split between [[and]], but parameter expansion and command substitution occurs.

② support string pattern matching, using the = ~ operator even when the support shell regular expressions. You can put the right string comparison as a model, not just a string, such as [[hello == hell?]], The result is true. [[]] In the matching string or wildcard, no quotation marks.

③ using [] [...] conditional structure, instead of [...], a number of logical errors can be prevented in the script. For example, &&, ||, <and> can be present normally in the operator [[]] conditional structure, but if present in [] structure, it will be given. For example can be used directly if [[$ a! = 1 && $ a! = 2]], double quotes if not applicable, compared to if [$ a -ne 1] && [$ a! = 2] or if [$ a -ne 1 -a $ a! = 2].

④bash the double brackets in expression as a single element, and returns an exit status code.

example:

if ($i<5)

if [ $i -lt 5 ]

if [$ 1 a -ne -a $ a! = 2]

if [ $a -ne 1] && [ $a != 2 ]

if [[ $a != 1 && $a != 2 ]]

for i in $(seq 0 4);do echo $i;done

for i in `seq 0 4`;do echo $i;done

for ((i=0;i<5;i++));do echo $i;done

for i in {0..4};do echo $i;done

Third, braces, braces {}

1, the conventional usage

① braces expansion. (Wildcard (globbing)) braces will do filename extension. In braces, it does not allow empty, unless this gap is quoted or escaped. The first: to a comma-separated list of files to braces to expand. The touch {a, b} .txt result a.txt b.txt. Second: for the bit to braces (..) separated list of files from Extending the sequence, such as: touch {a..d} .txt result a.txt b.txt c.txt d.txt

# ls {ex1,ex2}.sh

ex1.sh ex2.sh

# ls {ex{1..3},ex4}.sh

ex1.sh ex2.sh ex3.sh ex4.sh

# ls {ex[1-3],ex4}.sh

ex1.sh ex2.sh ex3.sh ex4.sh

② code block, also known as internal group, this structure actually creates an anonymous function. And parentheses in command, the command within the braces will not open a new sub-shell to run that script can still use the remaining portion of the variable in parentheses. Separated by semicolons command in parentheses, the last one also must have a semicolon. {} Must be a space between the first command and the left parenthesis.

2, several special alternative structure

$ {Var: -string}, $ {var: + string}, $ {var: = String}, $ {var:? String}

① $ {var: -string} and $ {var: = string}: If the variable var is empty, a command line with the string to replace $ {var: -string}, otherwise the variable var is not empty, then var replaced with the value of the variable $ {var: -string}; for $ {var: = string} replacement rules and $ {var: -string} is the same, the difference is $ {var: = string} when var is empty, replace $ {var: = string} with the string while the string assigned to the variable var: $ {var: = string} usage is a very commonly used, it is determined whether a variable assignment, if not then assign it a default value.

② $ {var: + string} replacement rules and contrary to the above, i.e., only when var is not empty when it is replaced with a string, var is not replaced when empty or is replaced by the value of the variable var, i.e., a null value . (Since the variable var is an empty, so that the two statements are equivalent)

③ $ {var: string?} Replace rule is: If the variable var is not empty, the value of var is replaced by $ {var:? String}; var If the variable is empty, put the string to the standard error output, and exit from the script. We can use this feature to check if the value of the variable.

Supplementary expansion: In the above structure, the five alternative string is not necessarily a constant value, the value of another variable available or a commanding output.

3, four kinds of pattern matching replacement structure

Memory pattern matching method:

# Is to remove the left (on the left side of the keyboard # $ it)

% Is to remove the right side (the right side of the keyboard of $%)

% And # symbols are the smallest in a single match, two symbols are the same maximum matching.

$ {Var% pattern}, $ {var %% pattern}, $ {var # pattern}, $ {var ## pattern}

A first mode: $ {variable% pattern}, when this model, the shell variable in the lookup, to see whether it is an end of the pattern to the pattern, and if so, the contents of the command line in the variable on the right to match the shortest removed mode

The second model: $ {variable %% pattern}, when this mode, shell looks in variable, the end of the pattern to see if it is a given pattern, and if so, from the command line to remove the contents of the variable in the right side of the longest pattern matching

The third model: $ {variable # pattern} When this mode, the variable in the shell lookup, to see whether it is a start pattern to pattern, if so, the content of the variable in the command line to remove the left shortest matching pattern

The fourth mode: $ {variable ## pattern} When this mode, the variable in the shell lookup, to see whether it is an end of the pattern to the pattern, and if so, the content of the variable in the command line to the right of the longest removed match mode

These four modes will not change the value of the variable, which only use the * symbol in pattern matching, and %%%, and # ## have differences. Structure pattern support wildcard * means zero or more of any character,? Represents only matches an arbitrary character, [...] which matches the characters in brackets, [! ...] in brackets represent the characters do not match.

# Var = test case

# Echo $ var

testcase

# Echo $ {var% s * e}

testca

# Echo $ var

testcase

# Echo $ {var %% s * e}

to

# Echo $ {var #? E}

stcase

# Echo $ {var ##? E}

stcase

# Echo $ {var ## * e}

# echo ${var##*s}

e

# Echo $ {var ## test}

case

4, character string extraction and replacement

$ {Were: NUM}, $ {were: num1: num2}, where {$ / pattern / pattern}, where {$ // pattern / pattern}

A first mode: $ {var: num}, when this model, shell extraction in var num characters to the end of all characters. If num is a positive number, 0 from the left; if num is negative, extracting the string starts from the right, but must use a digital or spaces after the colon or the entire num parentheses, such as $ {var: -2} , $ {var: 1-3} or $ {var: (- 2)}.

The second mode: $ {var: num1: num2}, num1 is a position, num2 length. It indicates the start of extraction from the position num1 $ $ var string length $ num2 the substring. It can not be negative.

The third model: $ {var / pattern / pattern} shows a pattern var replaced with another first matching string pattern.

The fourth mode: $ {var // pattern / pattern} will be replaced with another pattern represented var all matches the pattern string.

[Hundreds root @ ~] # var = / home / hundreds

[Root @ centos ~] # echo $ var

/home/centos

[Root @ centos ~] # echo $ {var: 5}

/ CentOS

[Root @ centos ~] # echo $ {var: -6}

CentOS

[Root @ centos ~] # echo $ {var: (- 6)}

CentOS

[Root @ centos ~] # echo $ {var: 1: 4}

home

[Root @ centos ~] # echo $ {var / o / h}

/ HHME / CentOS

[Root @ centos ~] # echo $ {var // o / h}

/hhme/cenths

After four brackets, the symbol $

(1) $ {a} of a variable value, without ambiguity braces can be omitted.

(2) $ (cmd) command substitution, and `cmd` the same effect, the result is a shell command cmd is lost, too Shell version does not support some $ () command substitution form such as tcsh.

(3) $ ((expression)) and the same `exprexpression` effect, the calculated values ​​of the mathematical expression exp, where exp they meet the rules of operation to the C language, or even ternary operator can be calculated and the logic expressions.

Fifth, use

Execute multiple commands

(1) Single parentheses, (cmd1; cmd2; cmd3) to open a new Run cmd1, cmd2, cmd3 shell sub-sequence, each separated by semicolons command, the command can not last a semicolon.

(2) single braces, {cmd1; cmd2; cmd3;} cmd1 execution command sequence in the current shell, CMD2, CMD3, separated by semicolons between the commands, a command must be the last semicolon, a first command and between the left parenthesis must be separated by a space.

And {} for (), the symbol in parentheses redirection command affects only that section, redirected outside the parenthesis character command affects all brackets.

These are the small series to share with you today about what the role of various parentheses in Linux Shell in? Hope this article can help small partners are working on Linux-related work. For more Linux-related knowledge remember concern Marco education official website Oh!

Source: Utopia No. 2

blog.csdn.net/taiyang1987912/article/details/39551385

* Disclaimer: Content and images are from the network (part of the contents Edit), belongs to original author, as the source of information is incorrect or the rights, please contact us, or delete the authorization.

Guess you like

Origin www.cnblogs.com/woshijiuke/p/12378526.html