Guide |
At the beginning of learning Inux shell scripting, for its arithmetic and logical operations. It is estimated that many friends feel more difficult to accept. When using the special variable logical operator "[]", you must ensure that there is a space between the operator and the arithmetic. Arithmetic can only use: let,expr and other commands to complete. Today's statement of the double-brace "(())" structure is an extension of the arithmetic and assignment operations in the shell. |
How to use:
Syntax:
(expression 1, expression 2 ... ))
Features:
1, in the double bracket structure, all expressions can be like C language, such as: a++,b--, etc. 2. In a double-brace structure, all variables may not be added: "$" symbol prefix. 3, the double parenthesis can carry on the logical operation, arithmetic 4, the double bracket structure expands the for,while,if condition to test the operation 5, supports the multiple expression operation, each expression uses "," separates
usage Examples:
Extended Arithmetic
#!/bin/sh a=1;b=2;c=3; ((a=a+1)); Echo $a; a=$ (a+1,b++,c++); Echo $a, $b, $c
Running result:]# sh testsh.sh23,3,4 supports multiple expressions between the two-bracket structures, and then the C-language common operators such as subtraction are supported. If the double parenthesis Band: $, the expression value is obtained and assigned to the left variable.
Extended Logical Operations
#!/bin/sh a=1;b= "AB"; echo $ ((a>1?8:9)); ((b!= "a")) && echo "ERR2";((a<2)) && echo "OK";
Operating result:]# sh testsh.sh9err2ok
Extending Process Control statements (logical relationships)
#!/bin/sh num=100;total=0; For ((i=0;i<=num;i++));d o ((total+=i));d Oneecho $total, Total=0;i=0;while ((i=5050)); then echo "OK"; fi
Operation Result:]# SH testsh.sh50505050ok
With the double parenthesis operator: [[]],[],test logical operation, already let,expr can be thrown aside.]
Free to provide the latest Linux technology tutorials Books, for open-source technology enthusiasts to do more and better: http://www.linuxprobe.com/
The Linux Shell "(())" Double parenthesis operator uses the