Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

[Bash]Function returning a boolean

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 04-29-2009
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 11 July 2016, 2:55 PM EDT
Location: Toronto, Canada
Posts: 2,898
Thanks: 0
Thanked 134 Times in 118 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 Unix and Linux 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 Unix and Linux 04-29-2009
cfajohnson's Unix or Linux Image
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
 
Join Date: Mar 2007
Last Activity: 11 July 2016, 2:55 PM EDT
Location: Toronto, Canada
Posts: 2,898
Thanks: 0
Thanked 134 Times in 118 Posts

Did you try it?

Did it work?
Sponsored Links
    #5  
Old Unix and Linux 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 Unix and Linux 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 Unix and Linux 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 Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 06:03 PM
bash search function doze Shell Programming and Scripting 3 09-30-2008 06:06 PM
returning from a function alirezan Shell Programming and Scripting 2 08-18-2008 04:12 PM
create a thread from a returning function wolwy_pete Programming 3 05-08-2008 04:48 AM
string returning function jisc Programming 5 03-23-2006 10:35 AM



All times are GMT -4. The time now is 12:37 AM.