Use of Linux parentheses

Source: Internet
Author: User
Tags arithmetic

When writing shell scripts, parentheses, parentheses, and various usages are often used. One, parentheses, parentheses () 1, single parenthesis.①The command group. The commands in parentheses will be executed in a new sub-shell sequence, so the variables in parentheses cannot be used by the rest of the script. Multiple commands in parentheses are separated by semicolons, and the last command can have no semicolon, and there is no space between the commands and the parentheses. ②command replacement. Equivalent to ' cmd ', the shell scans the command line once, discovers the $ (CMD) structure, executes the cmd in $ (cmd) once, obtains its standard output, and then places the output in the original command. Some shells are not supported, such as TCSH. ③Used to initialize an array. such as: Array= (a b c d) 2, double parenthesis.①Integer extension. This extended calculation is an integer-type calculation and does not support floating-point types. (exp) structure expands and computes the value of an arithmetic expression, if the result of the expression is 0, the exit status code returned is 1, or false, and a non-0-valued expression returns an exit status code of 0, or "true". If the logical judgment, the expression exp is true is 1, false is 0.②as long as the operators and expressions in parentheses conform to the C-language arithmetic rules, they can be used in $ (exp) or even a trinocular operator. For different carry (such as binary, octal, hex) operations, the output is automatically converted to decimal. such as: Echo $ ((16#5f)) result is 95 (16 decimal)③the values of variables such as a=5 can also be redefined by simply using (()). ((a++)) to redefine $a to 6④Variables in double brackets can not use the $ symbol prefix.   Multiple expressions are supported in parentheses, separated by commas. II) brackets, brackets [] 1, single brackets.①Bash's internal commands, [and test are equivalent. If we don't specify the absolute path, we usually use Bash's own commands. The left bracket in the if/test structure is the command ID that calls test, and the right bracket is judged by the close condition. This command takes its arguments as a comparison expression or as a file test, and returns an exit status code based on the results of the comparison. The if/test structure does not have to be in the right bracket, but this is required in the new bash.②The comparison operators available in Test and [] are only = = and! =, both for string comparisons, not for integer comparisons, and for integer comparisons to use only the-eq,-gt form. The greater than sign is not supported either for string comparisons or for integer comparisons. If you really want to use, you can use the escape form for string comparisons, if you compare "AB" and "BC": [AB \< BC], the result is true, that is, the return status is 0. Logic and logic in [] or using-A and-o are represented. ③The character range. Used as part of a regular expression to describe a matching range of characters. The regular is not used within brackets as a test purpose.④In the context of an array structure, brackets are used to refer to the number of each element in the array.  2, double brackets.①[[is a key word for the Bash programming language.] is not a command, [[]] structure is more general than [] structure. There is no filename extension or word splitting between all characters in [[and]], but parameter extensions and command substitution occur. ②supports pattern matching of strings, and even supports the shell's regular expressions when using the =~ operator. String comparisons can be made to the right as a pattern, not just a string, such as [[Hello = = Hell]], the result is true. Matches a string or wildcard character in [[]] without the need for quotation marks. ③use [[...]] The conditional judgment structure, rather than [...], can prevent many logic errors in the script. For example,The &&, | |, <, and > operators can normally exist in the [[]] conditional judgment structure, but if they appear in the [] structure, you will get an error. ④bash sees expressions in double brackets as a single element and returns an exit status code. Three) curly braces, curly braces {} 1, general usage.①Curly brace expansion. (wildcard (globbing)) expands the file name in curly braces. In curly braces, white space is not allowed, unless the whitespace is referenced or escaped. First: Expand the comma-delimited list of files in curly braces. such as touch {a,b}.txt result is a.txt b.txt. The second type: in curly braces with dots (.. The sequential file list of the splits expands, for example: touch {A. D}.txt results for a.txt b.txt c.txt d.txt②code blocks, also known as internal groups, actually create an anonymous function . Unlike the commands in parentheses, the commands in curly braces do not run with a new child shell, meaning that the remainder of the script can still be used in parentheses. The commands in parentheses are separated by semicolons, and the last one must have semicolons. There must be a space between the first command and the opening parenthesis of the {}. &NBSP;&NBSP;&NBSP;&NBSP;2, special usage.      For example: ${var:-string}, ${var#*/}, ${var%%.*}, ${var:5:5}, and so on. Use of   $ () and ${} and $ (()) and (())  $ () and ${}: In Bash Shell, $ () and ' (anti-quotes) are used to do command-substitution (commands substitution). and $ () each shell can not be used, if you use bash2 words, certainly no problem ...  see ${} bar ... It's actually used for variable substitution. In general, $var is not the same as ${var}. But using ${} will be more precise in defining the range of variable names.   I'll use some examples to illustrate some of the psychic powers of ${}: Suppose we define a variable as: file=/dir1/dir2/dir3/my.file.txt we can replace it with ${} to get a different value: ${file#*/}: Take out the first one. /And its left string: dir1/dir2/dir3/my.file.txt${file##*/}: Take off the last/and its left string: my.file.txt${file#*.} : Take out the first one. And the string to the left: file.txt${file##*.} : Take out the last one. And its left string: txt${file%/*}: Take off the last bar/its right string:/dir1/dir2/dir3${file%%/*}: Remove the first/its right string: (null value) ${file%.*}: Take the last one. and its right string:/dir1/dir2/dir3/my.file${file%%.*}: Take out the first one. And to the right of the string:/dir1/dir2/dir3/my Memory method is: # is to remove the left (on the plate on the left)% is removed to the right (on the plate on the right side)   single symbol is the smallest match; two symbols are the maximum match. ${file:0:5}: Extracts the leftmost 5 bytes:/dir1${file:5:5}: Extracts the 5 consecutive bytes to the right of the 5th byte:/dir2  We can also replace the string in the value of the variable: ${file/dir/path}: Change the first dir to path:/path1/dir2/ Dir3/my.file.txt${file//dir/path}: Convert all dir to path:/path1/path2/path3/my.file.txt  using ${} You can also assign values for different variable states (no setting, null value, Non-null value): ${file-my.file.txt}: If $file is not set, use My.file.txt to return the value. (null and non-null values are not processed) ${file:-my.file.txt}: If the $file is not set or null, use My.file.txt to return the value. ${file+my.file.txt}: If the $file is set to a null or non-null value, the value is returned using My.file.txt. ${file:+my.file.txt}: If $file is a non-null value, My.file.txt is used to return the value. ${file=my.file.txt}: If $file is not set, My.file.txt is used to return the value, and the $file is assigned the value My.file.txt. (null and non-null values are not processed) ${file:=my.file.txt}: If $file is not set or null, use My.file.txt to return the value and assign the $file to My.file.txt. ${file?my.file.txt}: If $file is not set, the output is my.file.txt to stderr. (null and non-null values are not processed) ${file:?my.file.txt}: If $file is not set or null, the my.file.txt output to STDERR. (non-null values are not processed) the understanding above is that you must distinguish between the unset and null and the non-null of the three assignment states. In general,: null is not affected, if not with:, NULL is unaffected, if band: then even null is affected .  And oh, ${#var} can calculate the length of the variable value: ${#file} can be 27 because/dir1/dir2/dir3/my.file.txt is exactly 27 bytes ... The purpose of   $ (()): It is used for integer arithmetic. In bash, the integer operation symbol for $ (()) roughly has these: +-*/: "Add, subtract, multiply, divide" respectively. %: Remainder Arithmetic & | ^!: "And, or, XOR, not", respectively. Example: Wangnc>a=5;b=7;c=2;wangnc>echo $a + $b 5 + 7wangnc>echo $ ((A + b*c)) 19wangnc>echo $ (((a*b)/c)) 17WANGNC >echo $ (($a + $b * $c)) 19wangnc>  the variable name in $ (()), can be replaced with a $ symbol in front of it, or not, such as: $ (($a + $b * $c) can also get 19 results   Also, $ ( ) can also be used for different carry (such as binary, octal, hex) for the operation, but the output is only decimal: Echo $ ((16#2A)) result is 42 (16 decimal) for the purpose of    (()): In fact, pure use (()) You can also redefine the value of a variable or make a testing:a=5; ((a++)) $a can be redefined as 6a=5; ((a--)) is a=4a=5; b=7; ((a < b)) a return value of 0 (true) is obtained. Common test symbols for (()) are as follows: &LT;: less than;: Greater than <=: less than or equal to >=: greater than or equal to = =: equals! =: Not equal to

Use of Linux parentheses (go)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.