Operating System - HP-UX
1838469 Members
2822 Online
110126 Solutions
New Discussion

How can i exit a script when a sentence is true into a function?

 
SOLVED
Go to solution
Manuales
Super Advisor

How can i exit a script when a sentence is true into a function?

Hi ...
i have a script .. which is using a function as follows:

!#/usr/bin/sh
command1 ..
command2 ..
function ...
command3 ..
command4 ...

how can i do to finish the script when a sentence is true into function?
i mean ..
if i sentence is true into function, how can i break the script, i mean finishe it without run command3 and command4? with exit? with break? what command do i must use ....?

Thanks,
Manuales

5 REPLIES 5
Ivan Ferreira
Honored Contributor
Solution

Re: How can i exit a script when a sentence is true into a function?

In your function, after your contition verification, add

exit 0

Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
Ivan Ferreira
Honored Contributor

Re: How can i exit a script when a sentence is true into a function?

For example:

check(){
if [ "$1" = "bye" ]; then
echo "Bye Bye"
exit 0
fi
}
echo hello
echo hello
check hello
echo hello
echo hello
check bye
echo hello
echo hello


If you run the script you will get:

hello
hello
hello
hello
Bye Bye

That is, two hello before check, first check is false so won't exit, two more hello, second check is true so it exits and the last two hello are not printed.
Por que hacerlo dificil si es posible hacerlo facil? - Why do it the hard way, when you can do it the easy way?
A. Clay Stephenson
Acclaimed Contributor

Re: How can i exit a script when a sentence is true into a function?


There are many ways to accomplish this but here is one approach:

Your function should have an explicit return statement

myfunct()
{
typeset -i FSTAT=0

if [[ ${1} -gt 5 ]]
then
FSTAT=1
fi
return ${FSTAT}
} # myfunct


typeset -i STAT=0
command1
command2
myfunction 10
STAT=${?} # capture status of last command/function
if [[ ${STAT} -ne 0 ]]
then
exit ${STAT}
fi
command3
command4
If it ain't broke, I can fix that.
Manuales
Super Advisor

Re: How can i exit a script when a sentence is true into a function?

Thanks all, i'll try when i have arrived at home, if i have a problem i'll writte again ..

thanks !!! :)
Manuales
Super Advisor

Re: How can i exit a script when a sentence is true into a function?

THANKS ALL!!!!
THAT WORKED !!!

THANK YOU VERY MUCH !!!!
:0)

Manuales.