[Shell] shell operations

Source: Internet
Author: User
Arithmetic Operations

The arithmetic operations mentioned here refer to addition (+), subtraction (-), multiplication (*), Division (/), and remainder (%, you can add parentheses to change the priority and only perform operations on integers (Linux bash shell does not support floating-point calculation by default)

#! /Bin/bashval = 'expr 2 + 2' # the expr parameter must be an integer echo "total value: $ Val "A = 10b =-5C = 'expr $ A + $ B '# + an empty box, otherwise, enter a + B = 10 + 20 echo "A + B = $ C" c = 'expr $ A-$ B 'echo "a-B = $ C" c =' expr $ A \ * $ B '# You must add \ before * to realize multiplication, because * has other meanings echo "a * B = $ C" c = 'expr $ A/$ B 'echo "A/B = $ C" If [$ A = $ b] # there must be a space before and after [] and =, [] Then ECHO "A = B" fiif [$! = $ B] Then # Then needs to start another line. It cannot be in the same line as if echo "! = B "filet" sum = 3 + 6 "Echo $ sumsum = $ (7*8) echo $ sum
There are three ways to perform arithmetic operations in Linux bash shell:

1. Use the let Command

let "sum=3+5"    # sum <- 8let "sum=3*5"    # sum <- 15 let "sum=2/5"    # sum <- 0 let "sum=11/5"   # sum <- 2 let "sum=11%5"   # sum <- 1 let "sum=-6-9"   # sum <- -15let "sum=(-6-9)*5"  # sum <- -75

Note,No space is allowed on the right of the equal sign and on both sides of the operator and bracket.

Ii. Use the expr command

sum=`expr 2 - 5`     # sum <- -3sum=`expr 2 + 5`     # sum <- 7 sum=`expr 3 \* 5`    # sum <- 15sum=`expr 3 / 5`     # sum <- 0 sum=`expr 7 / 5`     # sum <- 1sum=`expr \( 2 - 3 \) \* 6`   # sum <- -6 sum=`expr 2+4`       # sum <- 2+4sum=`expr 2-4*6·     # sum <- 2-4*6sum=`expr 1-(5-8)`   # sum <- 1-(5-8)
Note: The asterisk (*) and the left parentheses (and the right parentheses) must be escaped using a backslash (\).. There must be spaces on the right side of expr and on both sides of operators and parentheses.If compact writing is used (compact format can not escape *, (,), the arithmetic expression is returned.

Iii. Use (...)

sum=$((3+5))           # sum <- 8 sum=$(( 3 - 5 ))       # sum <- -2 sum=$(( 3 * 5 ))       # sum <- 15sum=$(( 7 / 5 ))       # sum <- 1 sum=$(( 7 % 5 ))       # sum <- 2 sum=$(( (1 - 2 )  * 4 ))   # sum <- -4

This writing method is relatively free. You can use a loose or compact format without escaping operators and parentheses.

Boolean operation
#! /Bin/Sha = 10b = 200 #-O or operation or #-A and operation andif [$! = $ B] Then ECHO "$! = $ B: A is not equal to B "fiif [$ A-lt 100-A $ B-GT 15] Then ECHO" $ A-lt 100-A $ B-GT 15: returns true "fiif [$ A-lt 100-o $ B-GT 100] Then ECHO" $ A-lt 100-o $ B-GT 100: returns true "fi


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.