(())双圆结构扩展

(())双圆结构扩展

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 ~]# 

转载请说明出处内容投诉
CSS教程_站长资源网 » (())双圆结构扩展

发表评论

欢迎 访客 发表评论

一个令你着迷的主题!

查看演示 官网购买