Seeking clarification: function within if conditional
So I'm trying to understand exactly what happens if you do as mentioned in title...my example:
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?
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.
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.
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.
Quote:
Originally Posted by strategery
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....?
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...
Hi ,
I have to use HP-unix OS to get difference between 2 files.while i tried a piece of code in other OS(linux/unix) as below, it worked fine & the output is desired one.
diff --suppress-common-lines -y file_1 file_2 >d.txt
The same doesn't works in HP -unix. Any help shall be... (6 Replies)
Hello All,
I'm looking for some help with grepping for two specific strings in files with multiple lines. For instance, I have files and the content looks like this:
=====Start=====
Record:1
Field 1 = aaaaaaaaaa
Field 2 = bbbbbbbbbb
Field 3 = 1234567890
... (9 Replies)
Hello everybody,
How can I do seek several words with grep command ?
I didn't find how to do that in the man for I'm French.
Then I tried
man bash | grep -in control flow
But flow is interpreted as an input file
Then I tried
man bash | grep -in "control" -a "flow"
but doesn't seem to work... (4 Replies)
Hi,
I am a newbie in shell scripting. I want to get an expert help in solving a text processing issue.
The issue I am facing is that, in the below log file contents I need to extract each block of lines (it could be a single line also) based on some regular expression and store it in... (8 Replies)
hello everybody!
I m compiling some program with the g++ on AIX 5.3 and it needs some library that i didn't find them i am new with the AIX here is the compilation error :
g++ -Daix -fpic -o printps -lxercesc1_1 -L/oracle/OraHome/lib32/ -L/epost2/blitz/lib -lhmltods -lhmltops -lgeneric... (0 Replies)
I am trying to update a script which I had created to monitor tablespace usage.
Originally the sql spooled out to a text file anything with more than 75% used.
I have been asked to change this.
Now the sql must spool out all tablespaces.
The script I have to write should scan the file for... (4 Replies)
Can any1 please help me answer a couple of this question?
1) What is the process management of UNIX? (single task, multitasks, etc...)
2) What is the process management of Linux? (single task, multitasks, etc...)
3) What is the type of process of UNIX? (process, thread, etc...)
4)... (1 Reply)
Hi. I wanted to increase my knowledge base with unix. can you help me find a like that talks about luns, physical volumes, volume groups, logical volumes then mountpoints? i would appreciate it
thanks
Jigar (1 Reply)