if 语句和调用函数 if 使用 bash

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15668170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 23:30:42  来源:igfitidea点击:

if statement and calling function in if using bash

bashtestingif-statement

提问by JumpOffBox

I have written a function:

我写了一个函数:

check_log(){
    if [ -f "/usr/apps/appcheck.log" ]
    then
         return 1
    else
         return 0
    fi
}

Then I call this function in an "if" condition:

然后我在“if”条件下调用这个函数:

if [ check_log ];
then
    ........statements....
fi

Will this work? I am confused here because bash returns 0 on success and 1 on failure, but my function is returning 1and the condition is checking for 1/0, it gets 1and it should give failures, but in my shell script the condition is passing.

这会起作用吗?我在这里很困惑,因为 bash 成功时返回 0,失败时返回 1,但是我的函数正在返回1并且条件正在检查1/ 0,它得到1并且应该给出失败,但是在我的 shell 脚本中,条件正在通过。

Can anyone shed light on this issue?

任何人都可以阐明这个问题吗?

回答by John Kugelman

if [ check_log ];

When you use square brackets you're invoking the testcommand. It's equivalent to if test check_logwhich is shorthand for if test -n check_log, which in turn means "if "check_log"is not an empty string". It doesn't call your check_logfunction at all.

当您使用方括号时,您正在调用test命令。它相当于if test check_logwhich 是 的简写if test -n check_log,这又意味着“如果"check_log"不是空字符串”。它根本不调用你的check_log函数。

Change it to this:

改成这样:

if check_log;

By the way, the function could be more simply written as:

顺便说一下,该函数可以更简单地写为:

check_log() {
    ! [ -f "/usr/apps/appcheck.log" ]
}

The return value from a function is the exit status of the last command, so no need for explicit return statements.

函数的返回值是最后一个命令的退出状态,所以不需要显式的 return 语句。

回答by markeissler

As noted by @john-kugelman, one solution (and perhaps the most correct one) is to use the following syntax:

正如@john-kugelman 所指出的,一种解决方案(也许是最正确的一种)是使用以下语法:

if check_log;

But an alternative solution is:

但另一种解决方案是:

if [[ $(check_log; echo $?) -eq 0 ]];

My personal preference is the latter as it fosters consistency among conditional statements. But the downside is thatbecause it relies on command substitution, it will fork a child process (i.e. a subshell) where as the first method will not.

我个人更喜欢后者,因为它促进了条件语句之间的一致性。但缺点是,因为它依赖于命令替换,它会分叉子进程(即子shell),而第一种方法不会。

An interesting read on the subject can be found here:

可以在此处找到有关该主题的有趣读物:

When does command substitution spawn more subshells than the same commands in isolation?

什么时候命令替换会产生比相同命令更多的子shell?

Updated (2017-10-20): to strikeout my preference for the second method as these days I'm on a mission to prevent unnecessary forking. The syntax in the first solution is not as intuitive for non-shell programming, but it certainly is more efficient.

更新(2017 年 10 月 20 日):为了消除我对第二种方法的偏好,因为这些天我的任务是防止不必要的分叉。第一个解决方案中的语法对于非 shell 编程来说并不直观,但它肯定更有效。