shell script in the role of various types of brackets (Summary)

Original link: http://www.cnblogs.com/weizhixu/p/10120420.html

Skills Summary:

String comparative double brackets [[]]; comparative arithmetic single brackets [] - left space around

Arithmetic double parentheses (()); and the shell command output parentheses () - no space left

Quick replaced by curly braces {} - leave space around

Backtick command substitution plays a role in ``

 

----------------------------------------------------------------------------------------------------------------

Single parentheses ():

Open another command group - the contents of the small brackets will open a sub-shell operate independently; brackets semicolon connection, the last command is not required; each command and brackets without spaces

Get command output --a = $ (command), is equivalent to a = $ `command`, to obtain a variable output of a command to the

Initialize an array --array = (abcd)

Double parentheses (()):

Omitted $ signed arithmetic - for ((i = 0; i <5; i ++)); if (($ i <5)); a = 5; ((a ++)) may be $ a redefined 6; a plurality of support brackets expressions separated by commas. 

C language rules of operation - $ ((exp)), exp is in line with the rules of the C language operators, expressions

Stepped into system operation - binary, octal, hexadecimal computation time, all output is automatically converted into a decimal. Such as: echo $ ((16 # 5f)) found to be 95 (hexadecimal turn decimal)

Single brackets []:

String comparison - = == and!

Integer comparison - does not mean: -gt: greater than; -lt: less than; -eq: equal; -ne

Array index --array [0]

Double brackets [[]]:

String comparison - can be put on the right as a model, not just a string, such as [[hello == hell]?], The result is true. [[]] In the matching string or wildcard, no quotation marks.

Logical operators - Script prevent many logic errors, such as, &&, ||, <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].

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

IF (I $ <. 5)
IF [-LT-I $. 5]
IF [-ne. 1 -a $ A $ A! = 2]
IF [$ A -ne. 1] && [$ A! = 2]
IF [[$ ! &&. 1 = A 2 = A $]]!

for in $ I (SEQ. 4 0); do echo $ I; DONE
for I 0 in SEQ `4`; do echo $ I; DONE
for ((I = 0; i <5; i ++)); do echo $ i; DONE
for i in {0..4}; do echo $ i; DONE
braces {}:
create an anonymous function - will not open a new process, the remaining variables in brackets It can still be used. 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.

特殊 替换 - $ {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 is replaced with the value of var $ {var: -string}; difference is $ {var: = string} var commonly used for determining whether the assignment, it is not to assign a default value on the var.

      ② $ {var: + string}: replacement rules and contrary to the above, i.e., only when var is not empty when it is replaced with a string, or if not replaced var empty is replaced by the value of the variable var, i.e. null . (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 is output to the standard error and exit from the script. We can use this feature to check if the value of the variable.

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

Alternatively pattern matching - $ {var% pattern}, $ {var %% pattern}, $ {var # pattern}, $ {var ## pattern}

# Is removed on the left (on the left side of the keyboard # of $);% is to remove the right side (on the right side of the keyboard% of $); and #% in a single symbol is a minimum matching, two symbols are the same maximum matching.

A first mode: $ {variable% pattern}. Find in shell variable, the end of the pattern to see if it is a given pattern, and if so, remove the right of the variable shortest matching pattern

 The second mode: $ {variable %% pattern}, when this model, the shell variable in the lookup, to see whether it is an end of the pattern to pattern, if it is, the variable in the right side of the longest matching pattern is removed

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, removing the variable of 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, removing the variable of the left longest matching pattern

 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 one match any character, [...] which matches the characters in brackets, [! ...] said they did not match brackets inside the characters.

TestCase var = #
# $ var echo
TestCase
# echo var {$ E% *} S
testca
# echo $ var
TestCase
# echo $ {var %% S *} E
TE
# echo $ {var #? E}
stcase
# echo $ ## var E {?}
stcase
# echo $ E} {var * ##

# ## * var echo $ {S}
E
# ## Test echo $ {var}
Case
string extraction and replacement - $ {var: num}, $ {var: num1 : num2}, $ {var / pattern / pattern}, $ {var // pattern / pattern}
a first mode: $ {var: num}, shell extraction of num in var character to the end of all the 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. You 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.

[the root @ CentOS ~] # var = / Home / CentOS
[the root @ CentOS ~] # echo $ var
/ Home / CentOS
[the root @ CentOS ~] # echo $ {var:. 5}
/ CentOS
[the root @ CentOS ~] # $ {var echo:} -6
CentOS
[CentOS the root @ ~] # echo $ {var: (-. 6)}
CentOS
[CentOS the root @ ~] # echo $ {var:. 1:}. 4
Home
[CentOS the root @ ~] echo $ {var # / O / H}
/ hhme / CentOS
[CentOS the root @ ~] # echo $ {var // O / H}
/ hhme / cenths
pair} and {(), the symbol in parentheses redirection command affects only that section, and the outside of the parentheses redirection symbol affects all commands in brackets.
-------------------------------------------------- -------------------------------------------------- ------------

Brackets after 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.

Reproduced in: https: //www.cnblogs.com/weizhixu/p/10120420.html

Guess you like

Origin blog.csdn.net/weixin_30612769/article/details/94783169