当前位置:网站首页>shell--变量的运算
shell--变量的运算
2022-07-27 05:22:00 【m0_69510295】
文章目录
变量的运算
在 Bash Shell 环境中,只能进行简单的整数运算,不支持小数运算
整数值的运算主要通过内部命令 expr 进行
运算符与变量之间必须有至少一个空格。
运算内容:加(+)、减(-)、乘(*)、除(/)、取余(%)
运算符号: ( ( ) ) 和 (()) 和 (())和[]
运算命令:expr和let
运算工具:bc(系统自带)
一、expr命令
expr命令(不仅可以运算,还支持输出到屏幕)
常用的 几种运算符
| 符号 | 含义 |
|---|---|
| + | 加法运算 |
| - | 减法运算 |
| * | 乘法运算,注意不能仅使用“*”符号,否则将被当成文件通配符 |
| / | 除法运算 |
| % | 求模运算,又称为取余运算,用来计算数值相除后的余数 |
举例
[[email protected] ct]# expr 1 + 1
2
[[email protected] ct]# expr 1+1
1+1
[[email protected] ct]# expr 2 * 2
expr: 语法错误
[[email protected] ct]# expr 2 \* 2 //乘法*需要转义;"\"是转义符
4
[[email protected] ct]# expr 2 '*' 2 //乘法也可以用单引号表示但没太大必要因为只有一个字符
4
1、expr的运算
expr不仅支持常量还支持变量的运算
1.不在脚本中运算
[[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.交互式运算
#!/bin/bash
#1.定义输出数字
read -p "请输入第一个数字:" num1
read -p "请输入第二个数字:" num2
#2.执行加法运算
expr $num1 + $num2
echo "求和数:$sum"
二、特殊符号变量的运算: [ ] 、 []、 []、(())
[ ] 和 []和 []和(()) 必须要和echo在一起用因为他只能运算无法输出结果
1、$[]整数运算
[[email protected]]# echo $[10*10] //$[]里的*不需要转义
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
- [ ] 变 量 的 运 算 , 可 省 略 [ ] 里 的 []变量的运算,可省略[]里的 []变量的运算,可省略[]里的
[[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] //遵循先乘除后加减,否则需要用括号括起来表示优先运算
75
[[email protected]]# echo $[(a+b)*c]
360
2、$(())整数运算
[roo[email protected]]# echo $((1+1))
2
[[email protected]]# echo $((5-2))
3
[[email protected]]# echo $((a-b))
7
[[email protected]]# echo $((b-a)) //可以有负数
-7
三、let的运算
let的运算可以改变变量本身的值,但不显示结果,需要echo,其他的运算方式可以做运算但不改变变量本身的值
量本身的值
[[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 //求幂,4的2次方
[[email protected]]# echo $n
16
[[email protected]]# let n++ //n自加1
[[email protected]]# let n-- //n自减1
[[email protected]]# echo $a
13
[[email protected]]# echo $[a++] //先输出再自增1,这时a的值已经变了
13
[[email protected]]# echo $a
14
[[email protected]]# echo $[++a] //先自增1再输出,所以直接输出了变化后的值
15
[[email protected]]# echo $a
15
四、bc运算
支持小数运算,但在脚本中不可直接使用否则会进入交互界面,可以用echo结合管道使用

- scale :可以指定小数点后几位
如
scale=3 #表示小数点后3位数
10/3
3.333
可以与bc一起使用
[[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还可以做逻辑运算
真为1,假为0
[[email protected] ~]# echo "2>2" | bc
0
[[email protected] ~]# echo "2==2" | bc
1
[[email protected] ~]# echo "2<2" | bc
0
扩展
常用的运算表达式:
i=$(expr 12 \* 5)
i=$((12 * 5))
i=[12 * 5]
let i=12*5
重要
i++ 相当于 i=$[$i+1]
i-- 相当于 i=$[$i-1]
i+=2 相当于 i=$[$i+2]
边栏推荐
- Seven sorting details
- Source code compilation and installation LNMP and discuz Forum
- Advanced ROS communication mechanism
- The problem that tqdm cannot display in a single line
- Install Wireshark correctly
- Programming learning records - Lesson 3 [first knowledge of C language]
- 哈希表简介
- 反射器中getattr,hasattr,delattr,setattr的使用
- ROS distributed communication
- Ram of IP core
猜你喜欢

Robot navigation implementation

Detailed explanation of thread safety problems

Wireshark packet modification -- IP address modification (I)

Interpretation of unity desktop version 7.6

Li Kou daily question (linked list simulation)

DNS域名解析服务

Unable to start program, access denied?

七大排序详解

Related knowledge of internal classes

基于Apache下ab工具进行网站压力性能测试
随机推荐
ROS workspace coverage
Constraints and design of database
DNS故障分析优化
Index and transaction of database (emphasis)
DNS域名解析服务
Related knowledge of internal classes
Unable to start program, access denied?
ROS node name duplicate
Comparison of communication mechanisms
Programming learning records - Lesson 6 [functions]
数据库的索引和事务(重点)
哈希表简介
iptables防火墙及SNAT和DNAT
Related knowledge of multithreading
面试常问的问题总结【呕心沥血熬了一个晚上总结的】
Function call of shell script
多线程常见锁的策略
Chapter for software testing
数组及下标索引
Tangent space and TBN matrix