当前位置:网站首页>Shell script programming - operation
Shell script programming - operation
2022-07-29 15:35:00 【51CTO】
算术运算
shell支持算术运算,仅支持整数运算
+(加法) -(减法) *(乘法) /(除法,整除) %(求余) **(乘方)
Multiplication is the same as wildcard,In some special scenarios, escaping is required
Arithmetic operation:
let var=算术表达式 ((var=算术表达式)) var=$[算术表达式] var=$((算术表达式))
var=$(expr arg1 arg2 arg3 ...) declare -i var=数字 echo '算术表达式’ | bc


随机数变量
$RANDOM 取值范围 0-32767

颜色函数
格式: echo -e "\033[显示格式;字背景颜色;字体颜色m文本\033[0m" 或者 echo "\e[显示格式;字背景颜色;字体颜色m文本\e[0m"
显示格式:0(默认值)、1(粗体/高亮)、4(下划线)、5(闪烁)、7(反显,翻转前景色和背景色)
增强型赋值:
i +=10 同于 i=i+10
i -=10 同于 i=i-10
*= /= %=
++i ,i++ 同于i=i+1
--i i-- 同于i=i-1


逻辑运算(转换为二进制计算)
true (1) false(0)
逻辑与(&):是且的关系,Both are true,和0相与结果为0
1 & 1=1 0&1=0 0&0=0 1&0=0
逻辑或(|):是或者的关系,Any value is true,和1or the result is1
1 |1 =1 1|0=1 0|0=0 0 |1=1
逻辑非(!):取反
!1=0 !0=1
逻辑异或(^0):XOR two values,相同为假(0),不同为真(1).两个数字x,yXOR the result as z,z再和xXOR is obtainedy,与y异或得到x
1^1=0 0^0=0 1^0=1 0^1=1
x^y=z z^x=y 计算方法 x^y^x得到0^y=y 任何值和0XOR is obtained as its own value

Use the XOR method,swap value
短路运算
短路与(&&): command1 &&command2 如果command1结果为真(1),则command2必须执行,才能得到最终结果
如果command1结果为假(0),则command2不需要执行,Because the end result is already0
与逻辑&的异同:相同点,The results obtained are all booleans,相同;不同点:Both sides of the logical AND need to be executed,Short circuit and don't need
短路或 (||):command1 &&command2 如果command1结果为真(1),则command2无需执行,Because the end result is already1
如果command1结果为假(0),则command2必须执行,to get the final result
与逻辑|的异同:相同点,The results obtained are all booleans,相同;不同点:Both sides of the logical AND need to be executed,short circuit or not required
边栏推荐
- 广汽本田安全驾驶体验营,老司机的必修课
- Replay Online Traffic Tool - GoReplay
- Uni drop-down selection menu function/lazy loading images
- Jmeter实现多用户测试
- uni 的下拉式筛选菜单的功能/图片懒加载
- [yolov7 series two] positive and negative sample allocation strategy
- Mysql数据库及表的建立
- 53 LeetCode 】 【. Most architectural array and
- 【 LeetCode 】 88. Merging two orderly array
- 【 LeetCode 】 350. The intersection of two arrays. II
猜你喜欢
随机推荐
Immediate experience with CTS - | D further promotion application equipment compatibility
【yolov7系列二】正负样本分配策略
【 LeetCode 】 217. Duplicate elements
Qt实战 | 如何获取USB设备信息?
回放线上流量利器-GoReplay
用Asm生成Class字节码文件
APP为什么用JSON协议与服务端交互:序列化相关知识
小学生学程序---百变服装
LeetCode·3.无重复字符的最长子串·滑动窗口
MySQL Index Common Interview Questions (2022 Edition)
【LeetCode】121. 买卖股票的最佳时机
c语言字符和字符串总结
【左连接】坑点
数据分析(二)
基于C语言实现一个社交系统
手摸手实现Canal如何接入MySQL实现数据写操作监听
Linux安装MySQL(超详细)
每日优鲜倒下,下一个是谁?
ES6 从入门到精通 # 10:Set 集合数据类型
ArcGIS Pro与ArcGis区别









