shell学习总结之算术运算方法
来源:互联网 发布:苹果锁屏挂机 知乎 编辑:程序博客网 时间:2024/04/30 00:02
shell算术运算方法总结.sh
#! bin/bash# -------------------------------------------------------------------------------# 文件名: shell算术运算方法总结.sh# 版 本: 1.0# 创建日期: 2014/02/11# 描 述: 算术运算是指加法(+), 减法(-),乘法(*),整除(/), 求余(%)四种运算,可以通过添加括号改变优先级# 作 者: 毕小朋# 邮 箱: wirelessqa.me@gmail.com# 博 客: http://blog.csdn.net/wirelessqa# -------------------------------------------------------------------------------function use_let(){ echo "=================== 使用let命令进行算术运算 =================== " echo "" echo "# 注意:等号右边以及运算符和括号的两边都不能有空格" echo "" let result=4+2 echo "let result=4+2 #加 <- $result" let result=4-2 echo "let result=4-2 #减 <- $result" let result=4*2 echo "let result=4*2 #乘 <- $result" let result=4/2 echo "let result=4/2 #除 <- $result" let result=4%2 echo "let result=4%2 #求余 <- $result" let result=(4-2)*2 echo "let result=(4-2)*2 #改变优先级 <- $result" echo ""}function use_expr(){ echo "=================== 使用expr命令进行算术运算 =================== " echo "" echo "# 注意:" echo "# 1.乘号(*), 左括号( ( ) , 右括号( ) )必须使用反斜杠(\)转义。" echo "# 2.expr右边以及运算符和括号的两边必须有空格; 如果采用紧凑的写法(紧凑格式可以不对*, (, )进行转义), 则返回算术表达式." echo "" result=`expr 4 + 2` echo "\`expr 4 + 2 \` #加 <- $result" result=`expr 4 - 2` echo "\`expr 4 - 2 \` #减 <- $result" result=`expr 4 \* 2` echo "\`expr 4 \* 2 \` #乘 <- $result" result=`expr 4 / 2` echo "\`expr 4 / 2 \` #除 <- $result" result=`expr 4 % 2` echo "\`expr 4 % 2 \` #求余 <- $result" result=`expr \( 4 - 2 \) \* 2` echo "\`expr \( 4 - 2 \) \* 2\` #改优先级 <- $result" echo ""}function use_brackets(){ echo "=================== (( ... ))进行算术运算 ===================" echo "" echo "注意:这种写法无需对运算符和括号做转义处理,也可以采用松散或紧凑的格式." echo "" result=$((4+2)) echo "result=\$((4+2)) #加 <- $result" result=$((4-2)) echo "result=\$((4-2)) #减 <- $result" result=$((4*2)) echo "result=\$((4*2)) #乘 <- $result" result=$((4/2)) echo "result=\$((4/2)) #除 <- $result" result=$(( 4 % 2 )) echo "result=\$(( 4 % 2 )) #求余 <- $result" result=$(( ( 4 - 2 ) * 2 )) echo "result=\$(( ( 4 - 2 ) * 2 )) #加 <- $result" echo ""}use_letuse_expruse_brackets
运行结果:
bixiaopeng@bixiaopengtekiMacBook-Pro Shell备忘录$ bash shell算术运算方法总结.sh=================== 使用let命令进行算术运算 ===================# 注意:等号右边以及运算符和括号的两边都不能有空格let result=4+2 #加 <- 6let result=4-2 #减 <- 2let result=4*2 #乘 <- 8let result=4/2 #除 <- 2let result=4%2 #求余 <- 0let result=(4-2)*2 #改变优先级 <- 4=================== 使用expr命令进行算术运算 ===================# 注意:# 1.乘号(*), 左括号( ( ) , 右括号( ) )必须使用反斜杠(\)转义。# 2.expr右边以及运算符和括号的两边必须有空格; 如果采用紧凑的写法(紧凑格式可以不对*, (, )进行转义), 则返回算术表达式.`expr 4 + 2 ` #加 <- 6`expr 4 - 2 ` #减 <- 2`expr 4 \* 2 ` #乘 <- 8`expr 4 / 2 ` #除 <- 2`expr 4 % 2 ` #求余 <- 0`expr \( 4 - 2 \) \* 2` #改优先级 <- 4=================== (( ... ))进行算术运算 ===================注意:这种写法无需对运算符和括号做转义处理,也可以采用松散或紧凑的格式.result=$((4+2)) #加 <- 6result=$((4-2)) #减 <- 2result=$((4*2)) #乘 <- 8result=$((4/2)) #除 <- 2result=$(( 4 % 2 )) #求余 <- 0result=$(( ( 4 - 2 ) * 2 )) #加 <- 4
微信公众帐号: wirelessqa
关于作者:
作者: 毕小朋 | 老 毕 邮箱: wirelessqa.me@gmail.com
微博: @WirelessQA 博客: http://blog.csdn.net/wirelessqa
1 0
- shell学习总结之算术运算方法
- shell算术运算方法总结
- shell的算术运算总结
- shell中的算术运算总结
- Shell学习笔记(7)-高级变量之算术运算
- Shell脚本学习笔记(三)--算术运算
- Linux Shell编程之算术运算
- shell 脚本编程之算术运算符
- 【转载】Linux Shell之四 算术运算
- Shell入门(六)之算术运算
- Shell入门(六)之算术运算
- shell算术运算
- shell算术运算
- shell中算术运算
- shell 中的算术运算
- shell脚本算术运算
- shell 中的算术运算
- Linux Shell 算术运算
- 小P的故事——神奇的换零钱
- IOS绘制线条、矩形的一般用法
- 将字符串B插入字符串A中最大字符前面
- RelativeLayout_相对布局 属性的描述
- Fiddler Web Debugger
- shell学习总结之算术运算方法
- 如何学习嵌入式bootloader
- uva - 1368 - DNA Consensus String(字符串)
- Bone Collector
- java.lang.OutOfMemoryError: PermGen space及其解决方法
- 机器学习第七课--SVM(2)拉格朗日算子
- C语言温习
- 弹出Div层
- qt安装