[Bash]Function returning a boolean | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

[Bash]Function returning a boolean

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-29-2009
dolphin06 dolphin06 is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 29 April 2009, 11:31 AM EDT
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
[Bash]Function returning a boolean

Hello all,

I would like to know if it is possible to return a the result of a boolean expression from a function like this

Code:
function()
{
  # some code
  return [ $nb1 -lt 100 ] || [ $nb2 -lt 100 ]
}

and what will be the return value ?

Thank you for help.
Sponsored Links
    #2  
Old 04-29-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 12 April 2014, 10:59 AM EDT
Location: Toronto, Canada
Posts: 2,875
Thanks: 0
Thanked 108 Times in 101 Posts


Code:
func()
{
  # ...
  [ $nb1 -lt 100 ] || [ $nb2 -lt 100 ]
}

The return code will be 0 (=success) if either nb1 or nb2 is less than 100.
Sponsored Links
    #3  
Old 04-29-2009
dolphin06 dolphin06 is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 29 April 2009, 11:31 AM EDT
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Alright thank you. I have another question though, now that i have my result is it possible to use it in another test in this way :


Code:
function_boolean
bool=$?
while [ $bool ] && [ boolean expression ]; do
  # ...
done

    #4  
Old 04-29-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 12 April 2014, 10:59 AM EDT
Location: Toronto, Canada
Posts: 2,875
Thanks: 0
Thanked 108 Times in 101 Posts

Did you try it?

Did it work?
Sponsored Links
    #5  
Old 04-29-2009
devtakh devtakh is offline Forum Advisor  
Unix Enthusiatic
 
Join Date: Oct 2007
Last Activity: 21 August 2013, 5:20 AM EDT
Location: Bangalore
Posts: 738
Thanks: 0
Thanked 7 Times in 7 Posts
Quote:
Originally Posted by dolphin06 View Post
Alright thank you. I have another question though, now that i have my result is it possible to use it in another test in this way :


Code:
function_boolean
bool=$?
while [ $bool ] && [ boolean expression ]; do
  # ...
done


Code:
bool=$?
while [ $bool ]

this will always be true. so you may want to compare like $bol > 0 or $bol =0 or something as you want.



Code:
bool=$?
while [[ $bool > 0 &&  true ]]; do
  # ...
done

cheers,
Devaraj Takhellambam
Sponsored Links
    #6  
Old 04-29-2009
dolphin06 dolphin06 is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 29 April 2009, 11:31 AM EDT
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Yes it tried, but didnt work the way i want, but i dont know where i m wrong even i m echoing some values.
Sponsored Links
    #7  
Old 04-29-2009
dolphin06 dolphin06 is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 29 April 2009, 11:31 AM EDT
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Alright its working now that i add the comparison operator.
Thanks guys.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
returning multiple values from a function in C Andrewkl Programming 8 01-20-2009 05:03 PM
bash search function doze Shell Programming and Scripting 3 09-30-2008 05:06 PM
returning from a function alirezan Shell Programming and Scripting 2 08-18-2008 03:12 PM
create a thread from a returning function wolwy_pete Programming 3 05-08-2008 03:48 AM
string returning function jisc Programming 5 03-23-2006 09:35 AM



All times are GMT -4. The time now is 09:33 AM.