尽管函数的退出状态为:,但测试用例失败
#!/bin/bash
fun() {
return 0
}
fun && echo "true" || echo "false" #result: true
[[ `fun` ]] && echo "true" || echo "false" #result: false
通过使用echo命令进行测试,我们可以看到退出状态并没有像我们预期的那样改变结果:
#!/bin/bash
echo "orange" 1>/dev/null && echo "true" || echo "false" #result: true
[[ `echo "orange "` ]] && echo "true" || echo "false" #result: true
echo 1>/dev/null && echo "true" || echo "false" #result: true
[[ `echo ` ]] && echo "true" || echo "false" #result: false
是什么导致了这种行为?
使用backticks,可以获得函数的输出。没有输出。测试CCD_ 1是"0";false";因为这测试了单个字符串参数的非空性。
在bash提示下查看help test
,我们可以看到:
String operators:
-z STRING True if string is empty.
-n STRING
STRING True if string is not empty. <== this is you