加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

什么是测试bash函数返回值的正确方法?

发布时间:2020-12-15 09:08:38 所属栏目:安全 来源:网络整理
导读:我想在if语句中测试一个bash函数返回值,如下所示: if [[ func arg ]] ; then … 但我得到错误消息如:条件二元运算符。 什么是正确的方法这样做? 是吗: if [[ $(func arg) ]] ; then ... 如果是退出代码,而不是结果,你可以使用 if func arg; then ...
我想在if语句中测试一个bash函数返回值,如下所示:
if [[ func arg ]] ; then …

但我得到错误消息如:条件二元运算符。

什么是正确的方法这样做?

是吗:

if [[ $(func arg) ]] ; then ...
如果是退出代码,而不是结果,你可以使用
if func arg; then ...

如果你不能让函数返回一个合适的退出代码(返回N),并且你必须使用字符串结果,使用@Alex Gitelman回答。

$ help if:

if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi

Execute commands based on conditional.

The if COMMANDS list is executed. If its exit status is zero,then the
then COMMANDS list is executed. Otherwise,each elif COMMANDS list is
executed in turn,and if its exit status is zero,the corresponding
then COMMANDS list is executed and the if command completes. Otherwise,
the `else COMMANDS’ list is executed,if present. The exit status of the
entire construct is the exit status of the last command executed,or zero
if no condition tested true.

Exit Status: Returns the status of the last command executed.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读