Shell Foundation--Numerical calculation of variables

Source: Internet
Author: User
Tags cos sin

Numerical Calculation of variables

1.$ ((expression))

(1). Experiment 1

[Email protected] ~_~ day4]# 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] ~_~ day4]# sh test.sh

A-b=4

A+b=8

A*b=12

A/b=3

A%b=0

a**b=36

(2). Experiment 2

[Email protected] ~_~ day4]# cat test2.sh

#!/bin/bash

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

[[email protected] ~_~ day4]# sh test2.sh 1 + 2

3

Note: "1 + 2" has a space and, if none, it is only passed to $

2. $[expression]

[Email protected] ~_~ day4]# Echo $[3+4]

7

3.let Assignment Expression

Equivalence with (()), but the latter is highly efficient

[Email protected] ~_~ day4]# i=10

[Email protected] ~_~ day4]# let i=i+1

[Email protected] ~_~ day4]# echo $i

11

4.expr-expression

Note: Operators and calculated values have spaces around them

(1). arithmetic

[Email protected] ~_~ day4]# cat expr.sh

#!/bin/bash

A=$1

B=$2

echo "a*b= ' expr $a + $b '"

echo "a-b= ' expr $a \* $b '"

[[email protected] ~_~ day4]# sh expr.sh 2 3

A*b=5

A-b=6

(2). determine file extension name

Used in: Ssh-copy-id file; (VIM ' which Ssh-copy-id ')

Usage:

[Email protected] ~_~ ~]# cat exprfile.sh

#!/bin/bash

If expr "$": ". *\.pub"; then

echo "is. pub file"

Else

echo "is not *.pub file"

Fi

[Email protected] ~_~ ~]# sh exprfile.sh test.pub

8

Is. pub file

[Email protected] ~_~ ~]# sh exprfile.sh test.pu

0

is not *.pub file

(3). determines whether a number is an integer

[Email protected] ~_~ day4]# cat isinteger.sh

#!/bin/bash

Expr + 1 &>/dev/null

If [$?-eq 0]

Then

echo "is Integer"

Else

echo "is not a Integer"

Fi

[[email protected] ~_~ day4]# sh isinteger.sh 3.9

is not a Integer

[[email protected] ~_~ day4]# sh isinteger.sh 3

Is Integer

(4). Calculating string Lengths

[[email protected] ~_~ day4]# echo ' expr length ' Hello '

5

There are other uses to view man expr

5.BC command

A calculator, installed with Yum; direct BC into the calculator; Supports floating point calculation.

(1). Interactive Environment

[Email protected] ~_~ day4]# BC

BC 1.06.95

Copyright 1991-1994, 1997, 1998, 2004, 2006 Free Software Foundation, Inc.

This is the free software with absolutely NO WARRANTY.

For details type ' warranty '.

2*3

6

3+7+5

15

3.4+5.6

9.0

(2). Add pipe character

[Email protected] ~_~ day4]# echo "3+5" |BC

8

(3). through Scale Specifying calculation Precision

[Email protected] ~_~ day4]# var=3.14

[Email protected] ~_~ day4]# var= ' echo ' scale=2; $var "|BC"

[Email protected] ~_~ day4]# echo $var

9.42

6.awk Command for calculation

Supports floating-point operations with log, sqr, cos, sin, and more

[[email protected] ~_~ day4]# var= ' echo ' $var ' |awk ' {printf ("%g", Sin ($)} "

[Email protected] ~_~ day4]# echo $var

0.841471

[[email protected] ~_~ day4]# var= ' echo ' $var 2 "|awk ' {printf ("%g ", cos ($1/$2))} '

[Email protected] ~_~ day4]# echo $var

0.97922

Shell Foundation--Numerical calculation of 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.