Handling return & exit statements


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Handling return & exit statements
# 1  
Old 08-01-2010
Handling return & exit statements

I have 2 shell scripts the primary one would load the other one which will have functions defined in it.


Script 1:
Code:
. /apps/bin/Script 2

function

if [ script2_returnvalue  = 0 ]
then
  continue...
  ....
fi


Script 2:
Code:
function()
{

while read ln
(
  if [ condition1 ]
  then
   ...
   ....
   else
    exit
   fi
)<file1

return 0
}

now my question is in the script 2, i need to have that return 0 statement or else after while loop finishes it is returning non zero value which is causing my script 1 to fail with out continuing. Also if my script invokes exit statement since i also have return statement 0 value after while loop it is causing script 1 to continue with out exiting. could you please tell me how to handle this situation is there any way i make a forceful exit in script 2 when i call the exit with out even control going to the return 0 statement, Thank you.

Last edited by Ariean; 08-01-2010 at 12:35 AM..
# 2  
Old 08-01-2010
This seems normal; exit returns the value of the last finished command unless otherwise specified, and without return, so does a function. The solution would be to fix the problems that cause commands inside function() to fail, not to ignore their return value and keep going.
# 3  
Old 08-01-2010
Don't use the exit statement in the function (you can guess why not):

Script 2:
Code:
function()

while read ln
do
  if [ condition1 ]
  then
    ...
    ....
  else
    return 1
  fi

done < file1

return 0

but in the main script:

Script 1:
Code:
. /apps/bin/Script 2

function

if [ $? -eq 0 ]
then
  continue...
  ....
else
  exit 1
fi

exit 0

# 4  
Old 08-02-2010
Would it be possible the control doesn't pass to script1 after the while loop finishes as below. If it is possible please let me know how to handle this situation, Thanks a lot.

script1
Code:
. /apps/bin/script2

function

if [ $? = 0]
then
....
else
...
fi

script2
Code:
function ()
{
while read ln
do

done<${file}
}

# 5  
Old 08-02-2010
Using exit after your while-loop would prevent it returning to "script1" - although, you did source script2, so it's actually part of script1 now, anyway.
Code:
function ()
{
while read ln
do

done<${file}
exit
}

Why would you wish to do that?

Which shell, are you using? function is a reserved word in bash and ksh, so I trust that's not the name of your function?
# 6  
Old 08-02-2010
yes you are correct i am not using reserved word function, i just put it as an example. i am using Korn shell, The problem i am facing is once the while loop finishes reading the file for sometime in script2, the control is not passing to the script1 which i don't want to happen, would it be possible? or my analysis is wrong? is any thing wrong in code i pasted?Thank you
# 7  
Old 08-02-2010
There's no reason why (if you're not using exit in the function), after the while-loop (and function) are finished control would not return to "script1".

It might be helpful if you posted the actual code - or a representative example - that doesn't work.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Return or exit codes apart from 0 have a meaning?

The standard return code when everything goes right is 0, but what about using any other number something happened? Are there "ranges" depending on the kind of problem you want to express or is totally up to the programmer? (3 Replies)
Discussion started by: Tribe
3 Replies

2. Shell Programming and Scripting

Need the difference between exit 1 & exit 7

Hi In one of the script I am seeing some thing like exit 7,exit 1,exit 2,exit 3,exit 9,exit6.What is the difference between all of this exit.Can anyone help here please (3 Replies)
Discussion started by: ginrkf
3 Replies

3. Shell Programming and Scripting

Problem with call of Java Programm & return code handling & output to several streams.

Hello Everybody, thanks in advance for spending some time in my problem. My problem is this: I want to call a java-Programm out of my shell skript, check if die return code is right, and split the output to the normal output and into a file. The following code doesn't work right, because in... (2 Replies)
Discussion started by: danifunny
2 Replies

4. Shell Programming and Scripting

Nested if statements with && and ||?

Hello, I'm a shell scripting noob and new to this forum as well. My question is can nested if statements be done with && and || instead and if it can be done can someone provide an example pls. Thanks in advance for the help (1 Reply)
Discussion started by: zomgshellscript
1 Replies

5. Shell Programming and Scripting

Not able to exit from case statements

Hi all, I wrote the following simple shell script to perform addition, subtraction, multiplication and division. In the below program, i am not able to exit from the script Shell Script ----------- #!/bin/sh bgcal() { cal="" echo "Enter the Option Number: \c" read cal if then... (3 Replies)
Discussion started by: uxpassion
3 Replies

6. Shell Programming and Scripting

command does not return exit status due to tee

Hi, I am using /bin/sh. I want to display the stdout and stderr on the terminal as well as save it in a file, so I'm using this command. gmake all 2>&1 | tee log But even if gmake fails, it's always giving 0 as exit status, i suppose because of tee. # false 2>&1 | tee Log # echo $? 0... (2 Replies)
Discussion started by: anand_bh
2 Replies

7. Shell Programming and Scripting

Gen. Question - Script calls multiple programs - Return Code Handling?

General Question: If a script calls multiple external programs (external to the script, but still on unix), where do the return codes go? Let's say one of external programs fails, does the entire script fail and send a non-zero return code to the job scheduling software, or is the return code sent... (1 Reply)
Discussion started by: jnanasakti
1 Replies

8. HP-UX

Return of EXIT status ( $? )

I have the question: How return the exit code from then assign : VAR=$(command ) for ex. VAR=$(ls ....) VAREXIT=$? echo $VAREXIT VAREXIT is equal to 0 if the directory exist or not exist. WHI?? if i execute the command direct from line-command , the value of $? is different if... (1 Reply)
Discussion started by: ZINGARO
1 Replies

9. UNIX for Dummies Questions & Answers

exit/return values

Sys: HP-UX 9000 In the calling script how do I 'read' the return/exit value of a called script?:confused: THX in advance for any assistence.:) (1 Reply)
Discussion started by: vslewis
1 Replies

10. UNIX for Advanced & Expert Users

Move command return with exit code of 2

I have a script which loads data files into Oracle and then moves each file into a 'processed' directory when each file has finished loading. Last night I found that the script was failing on the mv statement (with a return code 2) and the following message, mv: cannot access... (1 Reply)
Discussion started by: handak9
1 Replies
Login or Register to Ask a Question