Seeking clarification: function within if conditional

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Seeking clarification: function within if conditional
# 1  
Old 02-23-2017
Seeking clarification: function within if conditional

So I'm trying to understand exactly what happens if you do as mentioned in title...my example:
Code:
[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?
# 2  
Old 02-23-2017
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.
This User Gave Thanks to drysdalk For This Post:
# 3  
Old 02-23-2017
Quote:
Originally Posted by drysdalk
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.
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....?
# 4  
Old 02-23-2017
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.
# 5  
Old 02-23-2017
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....?
Code:
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

Last edited by Scrutinizer; 02-23-2017 at 05:04 PM..
# 6  
Old 02-23-2017
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...
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Seeking Alternative for diff in hp

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)
Discussion started by: vinil
6 Replies

2. Shell Programming and Scripting

Seeking help with search

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)
Discussion started by: bbbngowc
9 Replies

3. Shell Programming and Scripting

[grep] seeking more than one word

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)
Discussion started by: Oddant
4 Replies

4. Shell Programming and Scripting

seeking help in text processing

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)
Discussion started by: Alecs
8 Replies

5. Programming

Seeking Some libs for AIX 5.3

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)
Discussion started by: eternalflame
0 Replies

6. Shell Programming and Scripting

seeking help with shell script

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)
Discussion started by: Niadh
4 Replies

7. UNIX for Dummies Questions & Answers

Seeking help...Urgent!!! Please help me...

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)
Discussion started by: blind02002
1 Replies

8. Filesystems, Disks and Memory

seeking a doc on lvm etc...

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)
Discussion started by: jigarlakhani
1 Replies
Login or Register to Ask a Question