0xff = 255 [ian@pinguino ~]$ mycalc 29#37 29#37 = 94 [ian@pinguino ~]$ mycalc 64#1az 64#1az = 4771 [ian@pinguino ~]$ mycalc 64#1azA 64#1azA = 305380 [ian@pinguino ~]$ mycalc 64#1azA_@ 64#1azA_@ = 1250840574 [ian@pinguino ~]$ mycalc 64#1az*64**3 + 64#A_@ 64#1az*64**3 + 64#A_@ = 1250840574
对输入进行的额外处理超出了本技巧的范围,所以请小心使用这个计算器。
elif 语句非常方便。它允许简化缩进,从而有助于脚本编写。在清单 11 中可能会对 type 命令在
mycalc 函数中的输出感到惊讶。
清单 11. Type mycalc [ian@pinguino ~]$ type mycalc mycalc is a function mycalc () { local x; if [ $# -lt 1 ]; then echo "This function evaluates arithmetic for you if you give it some"; else if (( $* )); then let x="$*"; echo "$* = $x"; else echo "$* = 0 or is not an arithmetic expression"; fi; fi }
当然,也可以只用 $(( 表达式 )) 和 echo 命令进行 shell 算术运算,如清单 12 所示。这样就不必学
习关于函数或测试的任何内容,但是请注意 shell 不会解释元字符,例如 *,因此元字符不能在 (( 表达
式 )) 或 [[ 表达式 ]] 中那样正常发挥作用。
清单 12. 在 shell 中用 echo 和 $(( )) 直接进行计算 [ian@pinguino ~]$ echo $((3 + (4**3 /2))) 35
原文链接:http://www.ibm.com/developerworks/cn/linux/l-bash-test.html
上一篇:使用Portland改善Linux桌面移植性 下一篇:如何以Solaris架设FTP虚拟系统 |