Linux shell arithmetic operations detailed

Source: Internet
Author: User
This article is mainly to share with you the Linux shell arithmetic operation detailed, There are four ways to summarize the arithmetic operations of the Bash Shell and hope to help everyone.

1. Using Expr external program

Addition R= ' Expr 4 + 5 '
Echo $r

Attention! ' 4 ' + ' 5 ' there should be a space between the three

R= ' Expr 4 * 5 ' # error
Multiplication r= ' expr 4 \* 5 '

2. Use $ (())

r=$ ((4 + 5))
Echo $r

3. Use $[]

R=$[4 + 5]
Echo $r

Multiplication
R= ' Expr 4 \* 5 '
r=$ ((4 * 5))
r=$[4 * 5]
Echo $r

Division
r= ' Expr 40/5 '
r=$ ((40/5))
r=$[40/5]
Echo $r

Subtraction
r= ' Expr 40-5 '
r=$ ((40-5))
r=$[40-5]
Echo $r

Find remainder
r=$[100% 43]
Echo $r

(e.g. 2 of 3)
r=$ ((2 * * 3))
r=$[2 * * 3]
Echo $r
Note: Expr does not have a

4. Using the Let command

Addition:
n=10
Let N=n+1
echo $n #n =11

Multiplication:
Let m=n*10
Echo $m

Division:
Let R=M/10
Echo $r

To find the remainder:
Let R=m%7
Echo $r


By emerges:
Let R=m**2
Echo $r

Although the bash shell has four arithmetic operations, not each of which is cross-platform, it is recommended that expr be used.
In addition, we often have 1 operations in script, the following four methods are available:
m=$[m + 1]
m= ' expr $m + 1 '
m=$ (($m + 1))
Let M=m+1

Shell awk Loop summation

1. Simple summation

$ cat Test.txt 1122334455$ awk ' {sum + = $}; End{print sum} ' test.txt  165


2. Summation of specific columns

$ cat test.txt aa      11bb      22cc      33aa      44dd      55$ awk '/aa/{sum + = $ $}; END {print sum} ' test.txt 55


3. Sum, mean, maximum, minimum

$ cat test.txt aa      11bb      22cc      33aa      44dd      55$ cat test.txt | awk ' {sum + = $}; END {print sum} ' 165$ cat Test.txt | awk ' {sum + = $ '; END {print Sum/nr} ' 33$ cat Test.txt | awk ' BEGIN {max=0} {if ($2>max) max=$2 fi}; END {print Max} ' 55$ cat Test.txt | awk ' BEGIN {min=999999999} {if ($2<min) min=$2 fi}; END {print min} '

awk floating-point arithmetic:

Operation expression: awk ' begin{printf '%.6f\n ', (10/3)} '

Operation Result: 3.333333

PS: The internal arithmetic operators of the shell cannot handle floating-point numbers, so when you need to process floating-point numbers, use an external tool (such as awk)

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.