当前位置:网站首页>6、 Symbols and commands for numerical calculation of variables

6、 Symbols and commands for numerical calculation of variables

2022-06-24 01:40:00 jackxiao

1. Common arithmetic operators

Symbol

explain

+、-

plus 、 minus sign

*、/、%

Multiplication 、 Touch method 、 Remainder

**

Nether operation

++、--

Increase or decrease 、 Reduce

!、&&、||

Logic is not ( Take the opposite )、 Logic and (and)、 Logic or (or)

<、<=、>、>=

Comparative symbols ( Less than 、 Greater than )

==、!=、=

Comparative symbols ( equal 、 It's not equal 、 amount to )

<<、>>

Shift left 、 Shift right

~、|、&、^

According to the not 、 Exclusive or 、 And 、 or

=、+=、-=、*=、/+、%=

The assignment operation (a+=1 amount to a=a+1)

2. Common arithmetic operation commands

Symbol

explain

(())

Common operators for integer operations , It's very efficient

let

For integer operations , Be similar to "(())"

expr

Can be used for integer operations , There are many other features

bc

linux Calculator program under , Suitable for integer and decimal

$[]

For integer operations

awk

awk Can be used for integers , It can also be used for decimal operations

declare

Define variable values and properties ,-i Parameters can be used to define shaping variables , Do calculations

Two 、 Calculation practice

1. Double brackets

1) Routine usage

Symbol

explain

((i=i++))

Operation after assignment

((i=++i))

Evaluate first and then assign

i=$((i+1))

Assign values to variables after operation i

((a>7&&b<5))

Do a comparison operation , You can also make conditional judgments

echo $((2+1))

The result of the world output expression

++、-- Memory method of operation : i=i++ It means right first i assignment , And then do the self addition operation , That is, the actual i Value than output i Large value i=++i Indicates that the self addition operation is performed first , In the face of i assignment , That is, the actual i And the output of i equally

2)i++ and ++i test

  • i=6;echo $((i++));echo $i
6
7
  • i=6;echo $((++i));echo $i
7
7

2. Other calculation commands

1) let Assignment expression

let i=i+2 Equate to ((i=i+2))

2) bc Calculation command

bc yes linux Under the computer , The matching pipe is often used for calculation , You can perform decimal operations

echo "1+9"|bc
i=5;i=`echo $i+6|bc`

3) awk Do arithmetic

Decimals can be made 、 The operation of integers , useful echo "7.7 9.8"|awk '{print ($1+$2),($1*$2)}'

17.5 75.46

4) $[] Do calculations

echo $[4+2] $[4*2] $[4**2]

6 8 16

3、 ... and 、 Case study

1. Achieve output 1+2+3..+10=55 Calculation and output of

  • Method 1:
echo `seq -s "+" 10`=`seq -s + 10|bc`

1+2+3+4+5+6+7+8+9+10=55

  • Method 2:
echo `echo {1..10}|tr " " "+"`=`echo {1..10}|tr " " "+"|bc`

1+2+3+4+5+6+7+8+9+10=55

  • Method 3:expr
echo `seq -s + 10`=`seq -s " + " 10|xargs expr`

1+2+3+4+5+6+7+8+9+10=55

  • Method 4:(())
echo `seq -s + 10`=$((`seq -s + 10`))  

1+2+3+4+5+6+7+8+9+10=55

2.read Command to read parameters

1) read Command basis

  • grammar :read [ Parameters ] [ Variable name ] -p Set the prompt message -t Set the input waiting time ( second ) read -t 10 -p "input tow num:" a b

input tow num:1 2 Set up 10 Second timeout , The prompt is **

2) Calculation script Demo

  • cat /server/scripts/t.sh
#!/bin/bash
read -p "num1:" a
read -p "num2:" 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))"

After allowing scripts , Prompt the function of entering two strings respectively

3) The script adds a judgment integer statement

  • cat /server/scripts/t.sh
#!/bin/bash
#no.1
read -p "num1:" a
expr $a + 0 &>/dev/null
[ $? -ne 0 ] && {
    echo "pls int"
    exit 1
}
#no.2
read -p "num2:" b
expr $b + 0 &>/dev/null
[ $? -ne 0 ] && {
    echo "pls int"
    exit 2
}
#no.3
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))"

For each input string , Verify that it is an integer , If it is not an integer, prompt and exit , After both are integers , Then calculate

原网站

版权声明
本文为[jackxiao]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/11/20211116155619561s.html

随机推荐