argor

   :: 首页  :: 新随笔  :: 联系 ::  :: 管理

 

* 括号的用法

 

算数运算的表示


1.expr

cmdshell
expr 3 + 3 res=`expr 3 + 3`

expr 3 - 7

res=`expr 3 - 7`
expr 17 / 8 res=`expr 17 / 8`
expr 17 \*  3 res=`expr 17 \*  3`
expr \( 17 + 3 \) \* 2 / 4 res=`expr \( 17 + 3 \) \* 2 / 4`

 

  使用运算符时,一定要注意运算符左右两边要留有空格。shell中使用“反引号”把表达式括起来。乘法( * )运算、小括号需要使用“转义字符”。

 

2.$(( ))

  该运算无需对运算符和小括号使用转义字符;也不需要空格。

cmd

shell

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)))

 

[root@right mag]# echo $((3+3))
6
[root@right mag]# echo $((3*3+4*4))
25

 

3.$[ ]

  “同上”。

[root@right mag]# echo $[3+3]
6
[root@right mag]# echo $[3*3+4*4]
25

 

4.let

  使用let时,变量名不必使用$符号。

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

 

[root@right mag]# let score=80+7 && echo $score
87
[root@right mag]# let score=(20+60)*2 && echo $score
160

 

posted on 2017-11-28 12:27  argor  阅读(217)  评论(0编辑  收藏  举报