Help for exiting the function not script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help for exiting the function not script
# 1  
Old 06-05-2015
Help for exiting the function not script

Code:
function2()
{
 cmd1 
 cmd2
 cmd3
 ....
 cmdn
}

function2()
{
 cmd11
 cmd12
 cmd13
 ....
 ....

}

for i in {1,2}
do
    function$i 
done

If any of cmd$n failed in function1,it will exit function1, and don't excute other cmd in function1, and then jump to function2

Last edited by rbatte1; 06-05-2015 at 06:45 AM.. Reason: Moved end of CODE block marker to correct place
# 2  
Old 06-05-2015
You may be after return

You can also pass a return code:-
Code:
function1()
{
 cmd1 
 RC=$?
 if [ $RC -ne 0 ]
 then
   return $RC
 fi

 cmd2
 RC=$?
 if [ $RC -ne 0 ]
 then
   return $RC
 fi

 cmd3
 RC=$?
 if [ $RC -ne 0 ]
 then
   return $RC
 fi

 ....

 cmdn
 RC=$?
 if [ $RC -ne 0 ]
 then
   return $RC
 fi
}


I hope that this helps,
Robin

Last edited by rbatte1; 06-05-2015 at 06:42 AM.. Reason: Spread out CODE to be clearer
# 3  
Old 06-05-2015
Not for me. Works on linux and FreeBSD.

Your function definitions have a syntax error, on top of defining function2 twice.

---------- Post updated at 12:09 ---------- Previous update was at 11:42 ----------

Ohhh - it SHOULD exit if error. Try trap 'return $?' ERR in the functions.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 06-05-2015
There is no need for the rewrite of the catches all the time, could be simplified to:
Code:
cmd1 || return 1
cmd2 || return 1
...

This is acutaly just (about) the same as:
Code:
 cmd1 
 RC=$?
 if [ $RC -ne 0 ]
 then
   return $RC
 fi

Saying, if your only condition is to exit/return if any command fails, it could be simplified/short.
hth
# 5  
Old 06-05-2015
I suppose how it is decided that there is a failure.........
# 6  
Old 06-05-2015
Any (shell) command that returns with success (true) is 0.
Everything else is false, which is 1 or higher.

So wether you do:
Code:
cmd ; RET=$? ; [ 0 -ne $RET ] && return $RET
cmd && RET=0 || RET=1 ; [ 0 -ne $RET ] && return $RET
cmd || return 1

is equally...
# 7  
Old 06-05-2015
Quote:
Originally Posted by sea
There is no need for the rewrite of the catches all the time, could be simplified to:
Code:
cmd1 || return 1
cmd2 || return 1
...

This is acutaly just (about) the same as:
Code:
 cmd1 
 RC=$?
 if [ $RC -ne 0 ]
 then
   return $RC
 fi

Saying, if your only condition is to exit/return if any command fails, it could be simplified/short.
hth
Replacing
Code:
 cmd1 
 RC=$?
 if [ $RC -ne 0 ]
 then
   return $RC
 fi

with:
Code:
 cmd1 || return 1

compresses all of the possible error exit status information to a single value (1). The equivalent replacement (if you just care about the exit code) would be:
Code:
 cmd1 || return $?

If the code calling the function is depending on RC being set; it could be shortened to:
Code:
 cmd1 || return $((RC = $?))

(assuming that you're using a shell that supports POSIX arithmetic substitutions.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. Shell Programming and Scripting

Exiting a Unix script not working

Hi, I am trying to update a script so that it checks the current time, if the current time is 12am then I want the script to end and go no further. I have used the exit command but it doesn't seem to work. The section of script below is within another loop. I want the script to not just... (3 Replies)
Discussion started by: Izzy
3 Replies

4. 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

5. Shell Programming and Scripting

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.... (8 Replies)
Discussion started by: shellwell
8 Replies

6. Shell Programming and Scripting

shell script exiting before completing

I have a script which has the following statement, /opt/oracle/product/9i/bin/sqlplus << EOC >> $LOG_FILE 2>&1 username/password ---- Enters the SQL prompt @/export/home/oracle/shells/grant_userview.sql ---Runs the SQL script @/export/home/oracle/shells/grant_proc_userview.sql ---Runs the... (6 Replies)
Discussion started by: welldone
6 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

PHP5 Script 'Freeze' before exiting

I recently upgraded a system from php 4.4.2 to php 5.2.1, and one of my scripts has started behaving very strangely. I've tried google but come up blank so far. Basically what the script does is select a large amount of data from a mysql (4.1.21) database, do some manipulation, the plots a graph... (4 Replies)
Discussion started by: Unbeliever
4 Replies
Login or Register to Ask a Question