当前位置:网站首页>Operation of numerical variables and special variables

Operation of numerical variables and special variables

2022-07-23 05:55:00 LEE_ September

Operation of numerical variables and special variables

1. sketch

stay Bash Shell Environment , You can only do simple integer operations , Decimal operation is not supported

The operation of integer value is mainly through internal command expr Conduct

There must be at least one space between the operator and the variable

Operation content : Add (+)、 reduce (-)、 ride (*)、 except (/)、 Remainder (%)

Operation symbol :$ ( ( ) ) and $[ ]

Operation command ::expr and let

Computing tools : bc( System comes with )

2.expr command

expr Commands can not only calculate , It also supports output to the screen

Several commonly used operators :

+: Addition operation

-: Subtraction

*: Multiplication , Be careful not to use only "*" Symbol , Otherwise, it will be treated as a file wildcard

/: Division operations

%: Modular operation , Also known as remainder operation , Used to calculate the remainder after the division of numerical values

[[email protected] ~]# expr 1+1
1+1
[[email protected] ~]# expr 1 + 1
2
# There should be spaces between operators 
[[email protected] ~]# expr 2 * 2
expr:  Grammar mistakes 
[[email protected] ~]# expr 2 \* 2
4
[[email protected] ~]# expr 2 '*' 2
4
# Multiplication  *  Need to escape , You can also use single quotation marks 
[[email protected] ~]# expr $a + $b
5
[[email protected] ~]# expr $a \* $b
6
#expr It supports not only constants but also variable operations 
[[email protected] ~]# vim test.sh
#!/bin/bash
read -p " Enter the first number :" a
read -p " Enter the second number :" b
sum=`expr $a + $b`
echo $sum
# Sum up 
[[email protected] ~]# ./test.sh
 Enter the first number :5
 Enter the second number :15
20
[[email protected] ~]# ./test.sh
 Enter the first number :23
 Enter the second number :32
55

3. Operation symbol

$ ( () ) and $[ ] And echo Together with , Because it can only calculate and cannot output results

[[email protected] ~]# echo $((1+1))
2
[[email protected] ~]# echo $((2*2))
4
[[email protected] ~]# a=1
[[email protected] ~]# b=2
[[email protected] ~]# echo $((b-a))
1
[[email protected] ~]# echo $((10/8))
1
# Division only takes quotient 
$[ ]  Variable operation , Omission [ ] Inside $
[[email protected] ~]# echo $[(a+b)*c]
25

4.let operation

let The operation of can change the value of the variable itself , But it doesn't show the results , need echo, Other operations can be performed without changing the value of the variable itself

[[email protected] ~]# n=1;let n=n+1;echo $n
2
[[email protected] opt]# a=2
[[email protected] opt]# let $[a++]
# Express a+1, Omission 1
[[email protected] opt]# echo $a
3
# Find the area of a circle ,Π Value of 3
[[email protected] opt]# vim yuan.sh
#!/bin/bash
read -p " Please enter the radius of the circle :" r
let r=r**2
#r**2 Express r The square of 
m=`expr $r \* 3`
echo " The area of the circle is :$m"
[[email protected] opt]# ./yuan.sh
 Please enter the radius of the circle :5
 The area of the circle is :75

5.bc operation

Use bc Carry out operations , Support decimal operations , However, it cannot be used directly in the script, otherwise it will enter the interactive interface , It can be used echo Use in combination with piping

[[email protected] opt]# echo "scale=3;10/3" | bc
3.333
原网站

版权声明
本文为[LEE_ September]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207221756447752.html