(())双圆结构扩展

1.(())双圆结构扩展介绍

使用linux下(())双圆结构扩展并计算一个算术表达式的值时,如果表达式的结果为0,那么返回的退出状态码为1,或者是“假/false”;如果表达式的结果为一个非零值,那么返回的退出状态码为0,或者是“真/true”。这种情况和先前介绍的test命令和[]结构的行为正好相反。

2.(())双圆结构扩展样例

样例:

[root@elasticsearch ~]# vim double_circle.sh
[root@elasticsearch ~]# 
[root@elasticsearch ~]# 
[root@elasticsearch ~]# cat double_circle.sh 
#!/bin/bash

#(())结构可以用来计算并测试算术表达式的结果,退出状态将会与[]结构完全相反!

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

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

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

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

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


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


(( 1 / 2 ))  #除法的计算结果 < 1,截取之后的结果为0,$?的输出就为1.
echo "Exit status of \"(( 1 / 2 ))\" is $?."


(( 1 / 0 )) 2>/dev/null  #除数为0,非法计算.
echo "Exit status of \"(( 1 / 0 ))\" is $?."

exit 0
[root@elasticsearch ~]# sh double_circle.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@elasticsearch ~]# 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黑要上天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值