exiting from script

Hi,

I am trying to exit the script from a function.
I was in assumption that if we use exit ( inside or outside the function) it will exit from the script.

alternatively, return will exit from that particular function.

but in my case, exit is exiting from the function and not the script.

I am using exit in a subfunction.

is there any other way to completly exit from the script at any location?

Thanks.

How about you post what you have tried so we can look at it as well as real output and expected output.

Hi,

DoExit()
{
  if [ $# -eq  1 ]; then
        ExitCode=$1
        echo "FAIL"
        exit ${ExitCode}
else
        ExitCode=0
        exit ${ExitCode}
fi
}

F1 () {

some commands || DoExit 1

}
F1
echo "still here"

so, if some commads fails, I want to exit from the script.
but in the above case, I am still getting the messge "still here".

Hi, if I'm xell understanding, what you want is that if you have no arguments in your function DoExit, you exit the main program.
In this case, you can test the exit value after your function :

if (test $? -eq 0);then
your code for normal execution
else
exit 1
fi

$? return the value of the exit variable. e.g :

pwd % ll
total 108
all my files in my directory
coral22:kevin/res/test2 % echo $?
0
pwd % azerty
-bash: azerty: command not found
pwd % echo $?
127

hope it'll help...

Hi, Thanks for reply.

if i put directly exit. then also it is not exiting from the script.
further commands are still get executed.

some commands
if [ $? -eq 0 ];then
  exit
fi
echo "still here"

Can you post the script you wrote corresponding to "some command" plz?
Where does the parameter of your DoExit function comes from?

it is the sybase bcp command.

if for some reason ( say login incorrect ), the bcp fails, I dont want to move futher.

bcp $DB..table1 in file1 -c -t, -S${server} -U${user} -P${pass}  || DoExit 1
echo "still here"

here is the script I wrote :

#!/bin/bash
stty erase ^H

testlogin()
{
local log=$0
if(test $login != papa);then
exit 1;
fi
}

echo -n "login? "
read login
testlogin $login
echo"still here"
exit 0;

and here is what it give as an output :

pwd % prout.sh
login?  papa
still here
pwd % echo $?
0
pwd %
pwd %
pwd % prout.sh
login?  azerty
pwd % echo $?
1
pwd % 

It seems it is what you want.

You can add password and server as parameter for the function to test them in the same time (maybe rename the function testID then..)

Regards

it never exited because the code DoExit 1 was never executed. Its dependent on some commands's exit code. try playing with them

echo test || DoExit 1
echo test && DoExit 1

Here's the logic

exitcode=0 || ignore DoExit1
exitcode!=0 || execute DoExit1