当前位置:网站首页>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
边栏推荐
- Dom4j解析XML文件,处理来至XML的数据信息
- Common test case methods
- OpenGL create a new window
- Grasp interface automation in simple terms
- Software life cycle model ----- V model
- Software life cycle -- W model
- 测试案例:水杯,保温杯,黑板,抽纸,电梯,签到
- Day03 --- test case expansion
- Other tests: regression test, smoking test, random test
- Appium environment deployment for mobile terminal testing [unfinished to be continued]
猜你喜欢

Day 2 summary and test case operation

beanshell处理json的技巧

How OpenGL handles errors

2021-06-03pip报错 ValueError: Unable to find resource t64.exe in package pip._vendor.distlib

Test point, summary of the first day

Process of using Zen

讀《高效閱讀法-最劃算的自我投資》有感

appium 使用

第三天总结&作业

使用pip使用报错:pip is configured with locations that require TLS/SSL
随机推荐
判断 Map 中是否包含指定的 key 和 value
Selenium基础知识 自动搜索
Day 3 summary & homework
echo音箱配对及操作方法
【无标题】
amber教程3.2:GPU查看和用pmemd引擎跑MD
Basic process of visitors
BeanShell 内置变量 ctx
BeanShell内置变量vars的使用技巧
appium自动化测试,实例分享
[Huang ah code] getting started with MySQL - 1. SQL execution process
Day04 -- Installation of Zen path
Selenium基础知识 控制浏览器的滚动条
Day03 --- test case expansion
[JMeter]响应内容中文乱码解决办法
About count=count++
存储过程
Millisecond upload batch attachments
Fundamentals of software testing
Shell编程规范与变量