exiting from script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting exiting from script
# 1  
Old 08-03-2009
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.

Last edited by shellwell; 08-03-2009 at 07:17 PM.. Reason: added subfunction part
# 2  
Old 08-03-2009
How about you post what you have tried so we can look at it as well as real output and expected output.
# 3  
Old 08-04-2009
Hi,


Code:
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".
# 4  
Old 08-04-2009
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 :

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

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

Code:
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...
# 5  
Old 08-04-2009
Hi, Thanks for reply.

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


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

# 6  
Old 08-04-2009
Can you post the script you wrote corresponding to "some command" plz?
Where does the parameter of your DoExit function comes from?
# 7  
Old 08-04-2009
it is the sybase bcp command.

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

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help for exiting the function not script

function2() { cmd1 cmd2 cmd3 .... cmdn } function2() { cmd11 cmd12 cmd13 .... .... } for i in {1,2} (7 Replies)
Discussion started by: yanglei_fage
7 Replies

2. Shell Programming and Scripting

Exiting from the script abruptly

Hi Team, Need your help for the below code snippet. I wrote a module to read the file names remote server using file name convention. Issue : My script is coming out from while loop without reading complete file. test1() { while read line do echo $line file_nm_convention=`echo... (3 Replies)
Discussion started by: gvkumar25
3 Replies

3. Shell Programming and Scripting

Exiting the script if the character is not recognized

Below is the script that i'm using but i'm getting an error, echo -n "Read the letter >(enter a or b or c) " read letter if || || ; then echo "unacceptable character" else echo "Character Accepted" fi if the character entered is not equal to a or b or c, the script should... (6 Replies)
Discussion started by: web2moha
6 Replies

4. Shell Programming and Scripting

Exiting from Minicom on a shell script

This is what I've tried: #!/bin/sh send sh send showifs send exit ! killall minicom My problem is that for some reason when I do this it doesn't give me the results of the prior commands sent like showifs So I suspect my syntax is wrong. (1 Reply)
Discussion started by: uradunce
1 Replies

5. Shell Programming and Scripting

Exiting out of the script

I have to write a script in ksh which again should call another script. Say A.ksh is calling B.ksh. Now in B.ksh if the condition we are checking for is true then we have to go back to the main script A.ksh or if the condition in B.ksh is false then we have to totally come out of the scripts. I... (1 Reply)
Discussion started by: vpv0002
1 Replies

6. Shell Programming and Scripting

Bash Script Not Exiting

I have a script planned for the Helpdesk to allow them to (on a couple of RHEL 3 / RHEL 5 servers) reset passwords, stop / start printers, and clear print queues. The appropriate sudo permissions were given to their accounts, and the individual functions all work just fine. The ability to move... (0 Replies)
Discussion started by: tearsong
0 Replies

7. Shell Programming and Scripting

Exiting a script

I have a script abc.sh. Its contents are as follows: (7 Replies)
Discussion started by: lassimanji
7 Replies

8. Shell Programming and Scripting

Script is not exiting from run mode.

Hi Folks. My script is not exiting after run though its working correctly please suggest. #!/bin/ksh trap '' HUP . /bin/functions config_env PATH=/bin:/usr/bin:/usr/local/bin:$EXEC_PATH:$ORACLE_HOME/bin MONTH=$(control_register month) YEAR=$(control_register year) DATE_NOW="Job... (1 Reply)
Discussion started by: Haque123
1 Replies

9. Shell Programming and Scripting

exiting from script

there are many script in my project.i am having a problem when i am trying to quit from child script.what is the command to wrap up all the parent script and calling script as well? exit 0 is not working.please help.... (1 Reply)
Discussion started by: arghya_owen
1 Replies

10. Shell Programming and Scripting

Exiting from script when error occurs

Hi Friends, Is it possible to exit nicely(ie, to echo a message with the error occurred) from a shell script(quiet a big one :)) once it encounter an error in between the lines? For example, in my script I am calling the command mkdir and sometimes (when the directory already exists) it... (4 Replies)
Discussion started by: Sreejith_VK
4 Replies
Login or Register to Ask a Question