当前位置:网站首页>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]
边栏推荐
- Basic concepts of software testing
- Header and source files in ROS
- Wireshark function introduction
- 文件内容的读写——数据流
- Basic file operation of cmder
- rsync远程同步
- Wireshark packet modification -- IP address modification (I)
- Interface test process and interview questions
- 面试常问的问题总结【呕心沥血熬了一个晚上总结的】
- Reading and writing of file content - data flow
猜你喜欢

ROS topic name setting

Unable to start program, access denied?

互联网简单协议概括

Summary of Internet simple protocol

Allow or prohibit connecting to a non domain and a domain network at the same time

regular expression

ROS distributed communication

Detailed explanation of thread safety problems

Knowledge supplement of multithreading

Unity hub login no response
随机推荐
兼容性测试知识点
Ram of IP core
Some descriptions and usage of database index
Unity shader overview
Simple understanding of network principle
Communication mechanism cases
Path to file
Wireshark function introduction
Wireshark IP address domain name resolution
数组及下标索引
Interface test process and interview questions
Source code compilation and installation lamp and discuz Forum
Strategies for common locks in multithreading
TF coordinate transformation
Binary tree - search tree
UnityShader-LowPoly
C language - Custom structure type
Remote sensing image recognition imaging synthesis
Shell脚本备份MySQL数据库
C language -- string operation function and memory operation function