BashScripts.org
http://bashscripts.org/forum/

How to exit a bash function from other function ?
http://bashscripts.org/forum/viewtopic.php?f=16&t=1074
Page 1 of 1

Author:  i_have_no_user [ Wed Apr 21, 2010 12:42 am ]
Post subject:  How to exit a bash function from other function ?

I want to exit my bash function from other function,How it can be done ?

eg :-

function func1 {
if [ $? -eq 0 ] ; then
echo "OK"
else
echo "NOT OK....exiting"
exit
fi
}
function func2 {
some command
func1
some command
func1
}
function func3 {
some command
func1
some command
func1
}
func2 # call function
func3 # call function


Now if my command from function 2 fails ,then function 1 will print "NOT OK" and exit the script
I want to exit from function2 only and continue with function3.

Author:  DarthWavy [ Wed Apr 21, 2010 4:18 am ]
Post subject:  Re: How to exit a bash function from other function ?

you don't want exit, you want return. like so;
Code:
function func1 {
  if [ $? -eq 0 ] ; then
    echo "OK"
  else
    echo "NOT OK....exiting"
    return
  fi
}

Page 1 of 1 All times are UTC - 6 hours
© 2000, 2002, 2005, 2007 phpBB Group • http://www.phpbb.com