Seeking clarification: function within if conditional

So I'm trying to understand exactly what happens if you do as mentioned in title...my example:

[user@pc /]$ nslookup www.google.com &>/dev/null;echo $?
0
[user@pc /]$ nslookup foo.google.com &>/dev/null;echo $?
1
[user@pc /]$ if nslookup www.google.com &>/dev/null;then echo pass;else echo fail;fi
pass
[user@pc /]$ if nslookup foo.google.com &>/dev/null;then echo pass;else echo fail;fi
fail

So, even though exit status is 0, the same function passes an if conditional test. So I assume using the function within the if conditional looks at a return value rather than exit status? A lot of people and docs on the matter seem to refer to exit status as a return, making these two seem synonymous when they aren't(?) Can someone please help clarify what the if conditional is actually checking?

Hi,

Firstly, it might be helpful to discuss the distinction between a command and a function. The Bash built-in evaluator 'if' is capable of evaluating both of these things. So it can test the result of either an external command (like 'nslookup' in your examples here), or the result of a function contained in a script. If it's testing an external command it will look at the exit value, and if it's an internal function it will be the return value of the fuction.

So in your case, when the external command 'nslookup' has an exit status of 0 the 'if' check passes as true, and when it has an exit status of 1 it checks as false. In turn, 'nslookup' itself will exit with 0 if it could resolve the hostname and nothing else went wrong, and with a non-zero value if it couldn't resolve the hostname or if it encountered some other problem along the way that stopped it from doing so.

Hope this helps.

1 Like

Correct me if I mis-understand, but you're saying that basically the 'if' evaluator of bash makes the determination that it has been passed a command, and that an exit status of '0' should equate to true for that conditional? In other words, I think I've been viewing 'if' as a pure boolean test, when in fact it's much smarter....?

Please be aware that in (most?) shells, 0 is TRUE and 1 is FALSE , which is reversed from what you know in languages like Pascal, C, etc, or tools like awk .

The if conditional construct check the exit status of the
command or set of commands that it executes. A command that successfully executes without error returns a zero exit status.

If this value is 0, then the "then" commands will be executed. If the return code is anything else (i.e. an error condition occurred), then the "else" commands will be executed.

The if command shall execute a compound-list and use its exit status to determine whether to execute another compound-list.

The format for the if construct is as follows:

if compound-listthen
    compound-list[elif compound-listthen
    compound-list] ...
[else
    compound-list]


fi

The if compound-list shall be executed; if its exit status is zero, the then compound-list shall be executed and the command shall complete. 
Otherwise, each elif compound-list shall be executed, in turn, and if its exit status is zero, the then compound-list shall be executed and 
the command shall complete. Otherwise, the else compound-list shall be executed.

Shell Command Language: The if Conditional Construct

Thanks guys.
As RudiC pointed out, I was assuming I know how 'if' works from previous knowledge,
and Scrutinizer I went to the help for 'if' and see now. Years of programming elsewhere just got me all backwards...the answer was in the help though. Thanks guys, really. I've been confusing myself a lot writing some scripts lately. That's what happens when you assume...