Numerical calculation of shell-variables

Source: Internet
Author: User

The numerical computation of a variable is like a command.

(()), let, expr, BC, $[]

Here's a look at the operator of numeric calculations

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/8C/92/wKioL1hwVHaicj-lAAEicHK4YyY296.png-wh_500x0-wm_3 -wmp_4-s_3627575581.png "title=" 1.png "alt=" Wkiol1hwvhaicj-laaeichk4yyy296.png-wh_50 "/>

(()) This method is commonly used and efficient to perform integer-type calculations and does not support floating-point types.

[Email protected] ~]# ((a=1+2**3-4%3))

[Email protected] ~]# echo $a

8

[Email protected] ~]# b=$ ((1+2**3-4%3))

[Email protected] ~]# echo $b

8

[Email protected] ~]#

Where 2**3 is 2 of the 3-time Square

[[email protected] ~]# echo $ ((a++))

8

[Email protected] ~]# echo $a

9

[[email protected] ~]# echo $ ((a--))

9

[Email protected] ~]# echo $a

8

[[email protected] ~]# echo $ ((++a))

9

[Email protected] ~]# echo $a

9

[[email protected] ~]# echo $ ((--a))

8

[Email protected] ~]# echo $a

8

[Email protected] ~]#

As can be seen above, $ ((a++)) is the first output of a original value to add, and then we echo $a, is actually the output variable a after the value of the operation.

Memory method: Before the variable, the output variable value, the variable after the first place after the output variable value

The above are all integers, if there are decimal words will be an error, if you want to use the decimal Word can use the BC command

[email protected] ~]# cat test.sh

#!/bin/bash

A=6

b=2

echo "a-b=$ (($a-$b))"

echo "a-b=$ (($a + $b))"

echo "a-b=$ (($a * $b))"

echo "a-b=$ (($a/$b))"

echo "a-b=$ (($a * * $b))"

echo "a-b=$ (($a% $b))"

[Email protected] ~]# sh test.sh

A-b=4

A-b=8

A-b=12

A-b=3

a-b=36

A-b=0

If you want to implement the above operation by command-line arguments, you can modify the script to the following

#!/bin/bash

A=$1

B=$2

echo "a-b=$ (($a-$b))"

echo "a-b=$ (($a + $b))"

echo "a-b=$ (($a * $b))"

echo "a-b=$ (($a/$b))"

echo "a-b=$ (($a * * $b))"

echo "a-b=$ (($a% $b))"

Execute command

"Test.sh" 9L, 167C written

[[Email protected] ~]# SH test.sh 8 2

A-b=6

a-b=10

A-b=16

A-b=4

A-b=64

A-b=0

[[Email protected] ~]# SH test.sh 8 3

A-b=5

a-b=11

A-b=24

a-b=2

a-b=512

a-b=2

Now let's make a little calculator

[email protected] shell]# cat compute.sh

#!/bin/bash

echo $ (($1$2$3))

[Email protected] shell]# sh compute.sh 1+2

3

[Email protected] shell]#

Of course this is relatively simple, a lot of bugs we can look at other people write

http://chenhao6.blog.51cto.com/6228054/1232070

Some of the code is good to write, as follows

#!/bin/bash
echo "----------------------------------"
echo "| This is a simple integer calculator, author yuan|"
echo "----------------------------------"
Echo
While:
Do
Read-p "Please enter an integer:" Nu
Expr $nu + 0 &>/dev/null
If [$?-eq 0]
Then
echo "First number is $nu"
Break

Else
echo "The $nu you entered is not an integer, please re-enter"
Fi
Done

While:
Do
Read-p "Please enter two integers:" nu2
Expr $nu 2 + 0 &>/dev/null
If [$?-eq 0]
Then
echo "Second number is $NU2"
Break
Else
echo "The $nu2 you entered is not an integer, please re-enter"
Fi
Done
echo "------------------"
echo "| 1. Addition|"
echo "| 2. Subtraction | "
echo "| 3. Multiplication | "
echo "| 4. Division | "
echo "------------------"
Read-p "Please enter the algorithm you want to execute:" Me
Case $me in
"1")
sum= ' expr $nu + $nu 2 '
echo "$nu + $nu 2= $sum"
;;
"2")
jian= ' Expr $nu-$nu 2 '
echo "$nu-$nu 2= $jian"
;;
"3")
chen= ' expr $nu \* $nu 2 '
echo "$nu * $nu 2= $chen"
;;
"4")
chu= ' expr $nu/$nu 2 '
echo "$nu/$nu 2= $chu"
Esac


This article is from "Love Zhou Yu" blog, please be sure to keep this source http://izhouyu.blog.51cto.com/10318932/1889980

Numerical calculation of shell-variables

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.