LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 02-16-2012, 08:18 AM   #1
cmosentine
LQ Newbie
 
Registered: Feb 2012
Posts: 14

Rep: Reputation: Disabled
returning boolean from function ?


Hi all: Being new to BASH scripting, I am having a hard time getting my head around boolean usage. Below is some code that does not work properly. Looking at various online docs, it appears to me that this should work, but does not.

Thanks, Chris.


Code:
function get_host_file_list {
#do something in here

if [ #test results of above ]; then
	return 0  #true
else
	return 1  #false
fi
}

results=get_host_file_list(#parameters in here)
if [ $results ]; then
       #the if statement is always returning true
else
       #never gets here
fi

Last edited by cmosentine; 02-16-2012 at 08:20 AM.
 
Old 02-16-2012, 08:50 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.37, Debian Squeeze
Posts: 7,987
Blog Entries: 25

Rep: Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009
bash functions do not return values, they only set $? Thus
Code:
results=get_host_file_list(#parameters in here)
should return a syntax error.

How about
Code:
get_host_file_list(#parameters in here)
if [[ $? -eq 0 ]]; then
    echo "get_host_file_list returned 0"
else
    echo "get_host_file_list returned non-0"
fi
 
Old 02-16-2012, 09:05 AM   #3
cmosentine
LQ Newbie
 
Registered: Feb 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Thanks for the reply. I used your suggestion but changed it a bit to make it easier to read (for me that is). In my function I echo back a 1 or 0 and store it in a variable. I then logically test that variable ( x=0 ) and branch accordingly. Yes, I know it is more steps and possible a extra microsecond of cpu time, but I can easily read it. Maybe when I am more familiar with scripting I can be more efficient.

Thanks for the help.
 
Old 02-16-2012, 10:27 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Slackware 13.37, Debian Squeeze
Posts: 7,987
Blog Entries: 25

Rep: Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009Reputation: 1009
That's a valid way for a bash function to "return" data -- to write it into a global variable.

But if all you want is a succeed/fail then you may like to know about the bash idiom
Code:
if function_name arg1 arg2; then
    Do things you want if the function returned 0
else
    Do things you want if the function returned non-0
fi
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash catching the function returning value into a variable patolfo Linux - General 6 03-19-2010 11:58 AM
time() function not returning jiml8 Programming 5 04-09-2008 01:03 PM
returning different types of variables from a function knobby67 Programming 3 02-08-2008 08:53 PM
Perl - returning array from a function rose_bud4201 Programming 6 07-13-2007 01:02 AM
returning an array from a function.. javascript sonesay Programming 1 06-07-2004 05:28 AM


All times are GMT -5. The time now is 05:15 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration