当前位置:网站首页>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
List of articles
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
边栏推荐
- E-commerce platform project - "baseline" function list
- Contains方法,查看序列中是否包含某个元素
- Software testing is divided by stages: unit testing, integration testing, system testing, acceptance testing
- Day03 --- test case expansion
- SNAT与DNAT
- 判断 Map 中是否包含指定的 key 和 value
- Appium environment deployment for mobile terminal testing [unfinished to be continued]
- appium自动化测试,实例分享
- Test case: phone number
- Teach you how to interview and keep away from interview fear
猜你喜欢

Millisecond upload batch attachments

用jmeter对抽奖接口进行抽奖概率分析测试

echo音箱配对及操作方法

PTP Beidou time server (satellite clock server) helps the college entrance examination information system

BeanShell 内置变量 ctx
![Appium environment deployment for mobile terminal testing [unfinished to be continued]](/img/6d/e25206b88effaefc24d7cb4e246af0.png)
Appium environment deployment for mobile terminal testing [unfinished to be continued]

Test case: phone number

Selenium基础知识 自动登录百度贴吧

Jmeter正则表达式提取器

jmeter常见问题
随机推荐
Software testing is divided by stages: unit testing, integration testing, system testing, acceptance testing
jmeter常见问题
判断 Map 中是否包含指定的 key 和 value
2021-06-03pip报错 ValueError: Unable to find resource t64.exe in package pip._vendor.distlib
Test case: QQ login
Link: fatal error lnk1104: unable to open the file "opencv_world340.lib"
Millisecond upload batch attachments
OpenGL create a new window
idea中GBK代码转换成UTF-8格式 Ctrl+C+V一秒 解决单个代码文件转义 完美解决方法
Selenium基础知识 控制浏览器的滚动条
À propos du montage de fond, de la gestion des processus
Dom4j解析XML文件,处理来至XML的数据信息
Ie settings - solve the problem of uploading upgrade packages with drive letter paths
beanshell处理json的技巧
Day 2 summary and test case operation
测试点,第一天总结
Software testing process
爬取网页动态加载的评论
After class exercise 01 --- QQ login
Test case: phone number