当前位置:网站首页>Shell -- operation of variables
Shell -- operation of variables
2022-07-27 06:37:00 【m0_ sixty-nine million five hundred and ten thousand two hundre】
List of articles
Variable operation
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 (()) and (()) and []
Operation command :expr and let
Computing tools :bc( System comes with )
One 、expr command
expr command ( It can not only calculate , It also supports output to the screen )
frequently-used Several operators
| Symbol | meaning |
|---|---|
| + | 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 |
give an example
[[email protected] ct]# expr 1 + 1
2
[[email protected] ct]# expr 1+1
1+1
[[email protected] ct]# expr 2 * 2
expr: Grammar mistakes
[[email protected] ct]# expr 2 \* 2 // Multiplication * Need to escape ;"\" It's the escape sign
4
[[email protected] ct]# expr 2 '*' 2 // Multiplication can also be expressed in single quotation marks, but it is not necessary because there is only one character
4
1、expr Arithmetic
expr It supports not only constants but also variable operations
1. Do not operate in script
[[email protected] ~]# X=35
[[email protected] ~]# Y=16
[[email protected] ~]# expr $X + 5
40
[[email protected] ~]# expr $X + $Y
51
[[email protected] ~]# expr $X - $Y
19
[[email protected] ~]# expr $X \* $Y
560
[[email protected] ~]# expr $X / $Y
2
[[email protected] ~]# expr $X % $Y
3
2. Interactive computing
#!/bin/bash
#1. Define the output number
read -p " Please enter the first number :" num1
read -p " Please enter the second number :" num2
#2. Perform addition operations
expr $num1 + $num2
echo " Summation number :$sum"
Two 、 Operation of special symbolic variables : [ ] 、 []、 []、(())
[ ] and [] and [] and (()) And echo Used together because it can only calculate and cannot output the result
1、$[] Integer operation
[[email protected]]# echo $[10*10] //$[] Inside * There is no need to escape
100
[[email protected]]# echo $[10%8]
2
[[email protected]]# echo $[10/8]
1
[[email protected]]# echo $[10/12]
0
[[email protected]]# echo $[10%12]
10
- [ ] change The amount Of shipment count , can province A little [ ] in Of [] Variable operation , Omission [] Inside [] change The amount Of shipment count , can province A little [] in Of
[[email protected]]# echo $[$a+$b]
13
[[email protected]]# echo $[a+b]
13
[[email protected]]# echo $[a-b]
7
[[email protected]]# echo $[a*b]
30
[[email protected]]# echo $[a/b]
3
[[email protected]]# echo $[a%b]
1
[[email protected]]# echo $[a+b+c]
38
[[email protected]]# echo $[a+b*c] // Follow multiplication and division before addition and subtraction , Otherwise, it needs to be enclosed in parentheses to indicate the priority operation
75
[[email protected]]# echo $[(a+b)*c]
360
2、$(()) Integer operation
[[email protected]]# echo $((1+1))
2
[[email protected]]# echo $((5-2))
3
[[email protected]]# echo $((a-b))
7
[[email protected]]# echo $((b-a)) // There can be negative numbers
-7
3、 ... and 、let Arithmetic
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
Value of quantity itself
[[email protected]]# n=1;let n=n+1;echo $n
2
[[email protected]]# let n+=2 //n=n+2
[[email protected]]#
[[email protected]]# echo $n
4
[[email protected]]# let n=n**2 // Exponentiation ,4 Of 2 Power
[[email protected]]# echo $n
16
[[email protected]]# let n++ //n Self adding 1
[[email protected]]# let n-- //n Self reduction 1
[[email protected]]# echo $a
13
[[email protected]]# echo $[a++] // First output and then self increase 1, At this time a The value of has changed
13
[[email protected]]# echo $a
14
[[email protected]]# echo $[++a] // First of all, increase by yourself 1 Then the output , So the changed value is directly output
15
[[email protected]]# echo $a
15
Four 、bc operation
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

- scale : You can specify a few decimal places
Such as
scale=3 # After the decimal point 3 digit
10/3
3.333
It can be done with bc Use it together
[[email protected] ~]# a=10 [[email protected] ~]# b=3 [[email protected] ~]# echo "$a/$b" | bc 3 [[email protected] ~]# echo "scale=2;$a/$b" | bc 3.33
1、bc You can also do logical operations
How wonderful 1, False as 0
[[email protected] ~]# echo "2>2" | bc
0
[[email protected] ~]# echo "2==2" | bc
1
[[email protected] ~]# echo "2<2" | bc
0
Expand
Common operational expressions :
i=$(expr 12 \* 5)
i=$((12 * 5))
i=[12 * 5]
let i=12*5
important
i++ amount to i=$[$i+1]
i-- amount to i=$[$i-1]
i+=2 amount to i=$[$i+2]
边栏推荐
猜你喜欢

Wireshark IP address domain name resolution

Interpretation of unity desktop version 7.6

iptables防火墙及SNAT和DNAT

数据库在终端的基础操作

IP core summary

Communication mechanism cases

Li Kou daily question leetcode 513. find the value in the lower left corner of the tree

Random points in non overlapping rectangle (force deduction daily question)

shell编程规范与变量

About the use of TestNG related tags
随机推荐
Programming learning record -- recursively solving the tower of Hanoi problem
ArcGIS for JS API (1) get all field names of FeatureLayer
Linu性能调优:面对DDOS攻击,我们如何缓解局面?
Automatic tracking
Knowledge supplement of multithreading
Unit integration (grounding) test
Wireshark packet modification -- adding or modifying message fields (2)
Source code compilation and installation lamp and discuz Forum
ROS node name duplicate
C language - dynamic memory management
Programming learning records - Lesson 9 [operators]
Path to file
Programming learning records - Lesson 7 [functions]
正则表达式
C language minesweeping latest recursive expansion super detailed explanation (with source code)
Introduction to Wireshark graphical interface
Briefly remember the top ten orders
Interface test process and interview questions
Selenium knowledge points
Li Kou's first week's wrong question set