当前位置:网站首页>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]
边栏推荐
- C language minesweeping latest recursive expansion super detailed explanation (with source code)
- 反射器中getattr,hasattr,delattr,setattr的使用
- bug分类及缺陷和csv文件测试
- Wireshark graphical interface capture
- DNS域名解析服务
- 数据库在终端的增删改查
- Shell脚本一键配置LAMP
- LAMP--源码编译安装
- C language - file operation
- Shell programming specifications and variables
猜你喜欢

Wireshark IP address domain name resolution

Tangent space and TBN matrix

Remote sensing image recognition imaging synthesis

shell脚本之函数调用

Introduction to hash table

Unity shader overview

基于Apache下ab工具进行网站压力性能测试

Source code compilation and installation lamp and discuz Forum

shell脚本循环

Wireshark packet modification -- IP address modification (I)
随机推荐
Learning the operation environment needs to be equipped during software testing
Compatibility test knowledge points
PXE高效批量网络装机
Sexy prime number (acwing daily question)
数据库命令
测试基础概括
Learning records of programming -- Lesson 2 [first knowledge of C language]
Strategies for common locks in multithreading
数组及下标索引
Remote sensing image recognition imaging synthesis
Seven sorting details
Programming learning records - Lesson 6 [functions]
shell函数
Header and source files in ROS
单元集成(接⼝)测试
网络故障排查:Ping和Tracert命令
Basic file operation of cmder
DNS域名解析服务
Shell script if nested for loop script
Unityshader depth texture (understanding and problems encountered)