bash:如何评估双括号内命令的结果
被视为字符串而不是命令:
myfalse () {
return 0
}
mytrue () {
return 1
}
test-conditional ( ) {
SHOW="dont-show"
[[ ${SHOW} == "show" || mytrue ]] && echo "ok" || echo "wrong"
[[ ${SHOW} == "show" && mytrue ]] && echo "err" || echo "ok"
[[ ${SHOW} == "show" || myfalse ]] && echo "err" || echo "ok"
[[ ${SHOW} == "show" && myfalse ]] && echo "err" || echo "ok"
SHOW="show"
[[ ${SHOW} == "show" || mytrue ]] && echo "ok" || echo "err"
[[ ${SHOW} == "show" && mytrue ]] && echo "ok" || echo "err"
[[ ${SHOW} == "show" || myfalse ]] && echo "ok" || echo "err"
[[ ${SHOW} == "show" && mytrue ]] && echo "err" || echo "ok"
}
test-conditional
这显示了错误的输出:
ok
ok
err
ok
ok
ok
ok
err
我猜myTrue
和myfalse
因此, 双括号内的命令?
I guess mytrue
and myfalse
are considered strings instead of commands:
myfalse () {
return 0
}
mytrue () {
return 1
}
test-conditional ( ) {
SHOW="dont-show"
[[ ${SHOW} == "show" || mytrue ]] && echo "ok" || echo "wrong"
[[ ${SHOW} == "show" && mytrue ]] && echo "err" || echo "ok"
[[ ${SHOW} == "show" || myfalse ]] && echo "err" || echo "ok"
[[ ${SHOW} == "show" && myfalse ]] && echo "err" || echo "ok"
SHOW="show"
[[ ${SHOW} == "show" || mytrue ]] && echo "ok" || echo "err"
[[ ${SHOW} == "show" && mytrue ]] && echo "ok" || echo "err"
[[ ${SHOW} == "show" || myfalse ]] && echo "ok" || echo "err"
[[ ${SHOW} == "show" && mytrue ]] && echo "err" || echo "ok"
}
test-conditional
So this is showing wrong output:
ok
ok
err
ok
ok
ok
ok
err
(It should be ok
everywhere)
How can evaluate commands inside double brackets?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有
true
和false
返回返回代码的功能,您的最后一个条件需要校正。您可以使用:
还请注意如何将函数调用放置在
[[...]]
否则不会调用函数的情况下。在评估这些表达式时适用以下规则:
&&
之后的条件如果第一次条件失败,则不会评估。||
之后,如果第一个条件成功,则无法评估。You have
true
andfalse
functions returning opposite return codes and your last condition needs a correction.You can use:
Also note how function call is placed outside
[[ ... ]]
otherwise it won't invoke function.Following rules apply while evaluating these expressions:
&&
isn't evaluated if first condition fails.||
isn't evaluated if first condition succeeds.在bash
[[…]]
(以及大多数壳中的旧[…]
)内部,重要的是,如果测试的值是否具有某些字符:如果值测试如果空,测试失败:
对于变量也是如此:
so,
[[…]]
仅限于测试字符串而不是“退出代码”。您的功能定义“退出代码”而不是字符串。
您可以
myTrue
和myfalse
的值,为表示这些值的变量:并使用它们:
或,实际测试出口码外部
[[…]]
成语:一个(更复杂的)替代方案是使函数发出值并调用从
[[…]]
didiom中执行函数中的代码:一个(更复杂的)替代方案是使函数发出值,并从
[[…]]
idiom: ask kiss中 (可能)最简单的解决方案是最好的解决方案:
Inside a bash
[[…]]
(and inside the older[…]
in most shells) what matters is if the value tested has some characters or not:If the value tested if empty, the test fails:
That is also true for variables:
So,
[[…]]
is limited to testing strings not "exit codes".Your functions define "exit codes" not strings.
You may
define the values of
mytrue
andmyfalse
to be variables that represent those values:And use them:
Or, actually test exit codes outside a
[[…]]
idiom:One (more complex) alternative is to make the functions emit a value and call the execution of the code in the functions from the
[[…]]
idiom:KISS
But (probably) the simplest of solutions is the best solution: