shell算术运算方法总结
来源:互联网 发布:淘宝卖的猛犸象牙真伪 编辑:程序博客网 时间:2024/05/16 18:53
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
转自: http://blog.csdn.net/wirelessqa/article/details/19128405
阅读全文
0 0
- shell算术运算方法总结
- shell学习总结之算术运算方法
- shell的算术运算总结
- shell中的算术运算总结
- shell算术运算
- shell算术运算
- shell中算术运算
- shell 中的算术运算
- shell脚本算术运算
- shell 中的算术运算
- Linux Shell 算术运算
- shell算术运算
- Shell算术运算
- shell变量算术运算
- shell算术运算
- shell中的算术运算
- shell中的算术运算
- [shell]算术运算
- bzoj1105: [POI2007]石头花园SKA
- H5 12
- IMX6之openwrt使用dnsmasq实现广告过滤
- c++第六次上机实验
- H5 13
- shell算术运算方法总结
- grpc的java实现,从无到有
- C++第六次上机实验
- Linux 2
- Linux源码分析笔记1 (bootsect.s文件分析)
- intellij idea 中Python模块的扩展之读写excel文件的扩展
- H5 14
- MyBatis中的注解
- 浮点数类型计算问题,bigDecimal对象构造函数尽量使用字符串参数