The linux shell implements four arithmetic operations (integer and floating point). Simple method

  

When you just learn to write a shell batch, you need to do the basic operations in the logic operation: four arithmetic, here is a simple implementation in the linux shell. method.


1.Simple Method

[chengmo@centos5 ~]$ b=$((5*5+5-3/2)) [chengmo@centos5 ~ ]$ echo $b29


In the linux shell, we can use $(())
to put the expression in parentheses to achieve the function of the operation.


2. Other methods:

Using :expr to achieve the operation


[chengmo@centos5 ~]$ expr 5 - 41


Note: Write the expression that needs the operation after expr, and ensure that there is a space between the parameter and the operation symbol.


Category Syntax Description Conditional judgment expr1 \\|  Expr2 returns expr1 if expr1 is not zero or null, otherwise returns expr2. Expr1 \\& expr2 Returns expr1 if neither expr1 nor expr2 are zero or null, otherwise returns 0. The four-order operation expr1 + expr2 returns the value after expr1 plus expr2. Expr1 - expr2 returns the value after expr1 minus expr2. Expr1\\* expr2 returns the value after expr1 is multiplied by expr2. Expr1 /expr2 returns the value of expr1 except expr2. Expr1 % expr2 returns the remainder of expr1 except expr2. Size judgment expr1 \\> expr2 Returns 1 if expr1 is greater than expr2, otherwise returns 0. If both expr1 and expr2 are numbers, they are judged by the number size, otherwise they are judged by words. The following are the same. Expr1 \\< expr2 Returns 1 if expr1 is less than expr2, otherwise returns 0. Expr1 = expr2 Returns 1 if expr1 is equal to expr2, otherwise returns 0. Expr1 != expr2 Returns 1 if expr1 is not equal to expr2, otherwise returns 0. Expr1 \\>= expr2 Returns 1 if expr1 is greater than or equal to expr2, otherwise returns 0. Expr1 \\<= expr2 Returns 1 if expr1 is less than or equal to expr2, otherwise returns 0. Word processing expr1 : expr2 Compares a fixed string, which is a regular expression. The following characters can be used to assist:

. Matches one character.

$ Find the end of the string.

[list] Find any string that matches the list.

* Find 0 or more words before *.

\\( \\) Returns the string that matches the parentheses. .



3 floating-point operations:

[chengmo @ centos5 ~] $ expr 5.0 - 4 expr: Illegal Parameter


[chengmo@centos5 ~]$ echo $((5.0-4))-bash: 5.0-4: syntax error in expression (error token is ".0-4")< Br>


From the above results, it seems that the above expression is not enough to support floating-point operations. Looking at the data, I found out that bash does not support floating-point operations. If you need to perform floating-point operations, you need to use bc, awk processing.


Method 1:

[chengmo@centos5 ~]$ c=$(echo "5.01-4*2.0"| Bc)[chengmo@centos5 ~]$ echo $c-2.99

Method 2:

[chengmo@centos5 ~]$ c=$(awk 'BEGIN{print 7.01*5-4.01 }')[chengmo@centos5 ~]$ echo $c31.04


Note: $() is equivalent to `` in the shell. The middle contains command statement execution and returns the execution result.

Copyright © Windows knowledge All Rights Reserved