Five, Shell operations

Source: Internet
Author: User
Tags arithmetic echo command

Integer numeric Operations

use the expr command: You can only do integer operations and return the calculation results by default

Format:

Expr integer 1 operator integer 2 ...

Integer values can be supplied with variables, giving the result of the operation directly

+    addition   expr 43 + 21, expr $X + $Y-    subtraction expr 43-21, expr $X-$Y\*       multiplication C8>EXPR 43 \* 21, expr $X \* $Y/        Division expr 43/21, expr $X/$Y% ofthe       remainder expr 43 % 21, expr $X% $Y 

Example:

#!/bin/" Please enter the first number "" Please enter the second number  " num2sum = ' expr $num 1 +  '$num 1 + $num 2 = $sum"

use $[] or $ (()) Expressions

With echo output, the type of operation is analogous to expr multiplication * without the escape character when using a variable, specify the variable name directly without adding the $ symbol

How to perform arithmetic operations in the shell:
Let arithmetic expression let   c= $A +$B $[arithmetic expression]    C =$[$A +$B]$ ((arithmetic expression))    C=$ (($A +  $B)) expr arithmetic operation expression,
There should be a space between the operands and operators in the expression.
and to use a command to reference C= ' expr $A + $B '

Simplification of Expressions

increment, decrement, etc. of a variable

shorthand expression    Complete expression I+ +      i=i+1i--    i=i-1i*=2    i=i* 2 I+ =2     i=i+2i-=2      i=i-2i%= 2     i=i%2

Attention:

i++: First participate in other operations, after the operation ++i: first operation, and then participate in other operations

Example:

i=1echo $ ((+ +i)) J=1echo $ ((J+ +))

self-increment of variables/Reduction Operations

using the Let command

Operation variable value, only operation, not output results to see the results, you need to use the echo command let: Only do the operation does not return the results of the calculation, for the self-plus and decrement operations

#!/bin/Bashi=  i-=2echo $ilet i-=2------------ -------I=ten let i+= 2echo $ilet i+ =2echo $i

Five, Shell operations

Related Article

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.