linux 脚本 算术,【shell 脚本】算术测试需要使用(( ))

#!/bin/bash

# 算术测试.

# (( ... ))结构可以用来计算并测试算术表达式的结果.

# 退出状态将会与[ ... ]结构完全相反!

(( 0 ))

echo "Exit status of \"(( 0 ))\" is $?."         # 1

(( 1 ))

echo "Exit status of \"(( 1 ))\" is $?."         # 0

(( 5 > 4 ))                                      # 真

echo "Exit status of \"(( 5 > 4 ))\" is $?."     # 0

(( 5 > 9 ))                                      # 假

echo "Exit status of \"(( 5 > 9 ))\" is $?."     # 1

(( 5 - 5 ))                                      # 0

echo "Exit status of \"(( 5 - 5 ))\" is $?."     # 1

(( 5 / 4 ))                                      # 除法也可以.

echo "Exit status of \"(( 5 / 4 ))\" is $?."     # 0

(( 1 / 2 ))                                      # 除法的计算结果 < 1.

echo "Exit status of \"(( 1 / 2 ))\" is $?."     # 截取之后的结果为 0.

# 1

(( 1 / 0 )) 2>/dev/null                          # 除数为0, 非法计算.

#           ^^^^^^^^^^^

echo "Exit status of \"(( 1 / 0 ))\" is $?."     # 1

# "2>/dev/null"起了什么作用?

# 如果这句被删除会怎样?

# 尝试删除这句, 然后在运行这个脚本.

exit 0

======================

root@client.example.com ~/yang # ./calucate.sh

Exit status of "(( 0 ))" is 1.

Exit status of "(( 1 ))" is 0.

Exit status of "(( 5 > 4 ))" is 0.

Exit status of "(( 5 > 9 ))" is 1.

Exit status of "(( 5 - 5 ))" is 1.

Exit status of "(( 5 / 4 ))" is 0.

Exit status of "(( 1 / 2 ))" is 1.

Exit status of "(( 1 / 0 ))" is 1.

root@client.example.com ~/yang #

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/22664653/viewspace-689625/,如需转载,请注明出处,否则将追究法律责任。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值