您的位置:首页 > 运维架构 > Shell

shell中的算数运算

2014-03-13 10:20 337 查看
1 expr
2 $(( ))
3 $[ ]
4 let

M M M M M M M M M M M M M M
1 expr
cmdshell
expr 3 + 3res=`expr 3 + 3`
expr 3 - 7
res=`expr 3 - 7`
expr 17 / 8
res=`expr 17 / 8`
expr 17 \* 3res=`expr 17 \* 3`
expr \( 17 + 3 \) \* 2 / 4
res=`expr \( 17 + 3 \) \* 2 / 4`
使用运算符时,一定要注意运算符左右两边要留有空格。shell中使用“反引号”把表达式括起来。乘法( * )运算、小括号需要使用“转义字符”。

2 $(( ))
该运算无需对运算符和小括号使用转义字符;也不需要空格。
cmdshell
echo $((3+3))res=$((3+3))
echo $((3-3))res=$((3-3))
echo $((3*3))res=$((3*3))
echo $((3/3))res=$((3/3))
echo $((3*3+4*4))res=$((3*3+4*4))
echo $(((3+2)*7))res=$(((3+2)*7)))
3 $[ ]

“同上”。

4 let
使用let时,变量名不必使用$符号。
cmdshell
let score=80+7 && echo $scorelet score=80+7
let score=80-7 && echo $score
let score=80-7
let score=80*7 && echo $scorelet score=80*7
let score=80/7 && echo $scorelet score=80/7
let score=(20+60)*2 && echo $scorelet score=(20+60)*2
let score=(60-4)/7 && echo $scorelet score=(60-4)/7
*如果符号之间有空格,需要在表达式两边加上双引号。


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: