Using exit in For Loop - Is this acceptable

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Using exit in For Loop - Is this acceptable
# 1  
Old 03-09-2017
Using exit in For Loop - Is this acceptable

Hi Folks -

Here is a for loop I've created and I just wanted to see if this was okay practice:

Code:
for M in NAME1 NAME1 NAME3
do
    echo "Executing MaxL:" $M >>${_LOGFILE} 2>&1
    . ${_STARTMAXLPATH}startmaxl.sh ${_MAINPATH}${_MAXLPATH}$M.mxl 
    
    _RC=$?
    if [ $_RC -eq 0 ]
    then
        echo "$M : Successful" >>${_LOGFILE} 2>&1
        continue
    fi
        echo "Encountered Error in MaxL: $M" >>${_LOGFILE} 2>&1
        echo "Exiting Shell Session" >>${_LOGFILE} 2>&1
        echo  | mail -s "Encountered Error in MaxL: $M" z@z.com
        exit
done

echo "${_SN} has completed with no errors" >>${_LOGFILE} 2>&1
exit

Essentially, I want to exit entirely if my NAME*.mxl script returns a failure code. Therefore, I've added an exit in that section. Is this okay?

Thanks!
# 2  
Old 03-09-2017
Is correct, there is no problem.
You can also have
Code:
    if [ $_RC -eq 0 ]
    then
        echo "$M : Successful" >>${_LOGFILE} 2>&1
    else
        echo "Encountered Error in MaxL: $M" >>${_LOGFILE} 2>&1
        echo "Exiting Shell Session" >>${_LOGFILE} 2>&1
        echo  | mail -s "Encountered Error in MaxL: $M" z@z.com
        exit
    fi

Or
Code:
    if [ $_RC -ne 0 ]
    then
         echo "Encountered Error in MaxL: $M" >>${_LOGFILE} 2>&1
         echo "Exiting Shell Session" >>${_LOGFILE} 2>&1
         echo  | mail -s "Encountered Error in MaxL: $M" z@z.com
         exit
     fi
     echo "$M : Successful" >>${_LOGFILE} 2>&1


Last edited by MadeInGermany; 03-09-2017 at 02:15 PM..
This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 03-09-2017
That is fine, but, since an error occurred, I would use exit 1 for example, which exits with return code 1.
This User Gave Thanks to Scrutinizer For This Post:
# 4  
Old 03-09-2017
Thank you, both! Now I had another question if you dont mind. I'm trying to not repeat anymore code than I have to, therefore Im' trying to bundle a few commands into 1 for loop, with some conditions.

Here is what I'm trying to transform into 1 for loop

Code:
echo Disable connects, commands, and unload $essb_app >> $logfile 
$essbasepath/startMaxl.sh $maxl_script_path/dropLock.mxl $essb_user $essb_pswd $essb_srvr $essb_app $essb_db $maxllog >> $logfile 

echo Create $essb_app AS $essb_temp_app >> $logfile                        
$essbasepath/startMaxl.sh $maxl_script_path/createApplicationT.mxl $essb_user $essb_pswd $essb_srvr $essb_temp_app $essb_app $essb_db $maxllog >> $logfile 

echo Enable connects, commands, and reload $essb_app >> $logfile 
$essbasepath/startMaxl.sh $maxl_script_path/enableConnects.mxl $essb_user $essb_pswd $essb_srvr $essb_app $essb_db $maxllog >> $logfile

Notice, all have the same number of parameters getting passed except the second command. Therefore, I need to create a seperate section for that in the for loop.

Here is what I have so far and It's not working quite right. Any suggestions:

Code:
for M in dropLock createApplicationT enableConnects
do
    if [ $M != createApplicationT ]
    then
    echo "Executing MaxL: $M"
    . $essbasepath/startMaxl.sh $maxl_script_path/$M.mxl $essb_user $essb_pswd $essb_srvr $essb_app $essb_db $maxllog >> $logfile 
    
        _RC=$?
        if [ $_RC -eq 0 ]
        then
            echo "$M : Successful"
            continue
        fi
    fi
    . $essbasepath/startMaxl.sh $maxl_script_path/$M.mxl  $essb_user $essb_pswd $essb_srvr $essb_temp_app $essb_app $essb_db $maxllog >> $logfile 
        
        _RC=$?
        if [ $_RC -eq 0 ]
        then
            echo "$M : Successful"
            continue
        fi
    
    echo "Encountered Error in MaxL: $M"
    echo "Exiting Shell Session"
    echo  | mail -s "Encountered Error in MaxL: $M" z@z.com
    exit 1
done

echo "${_SN} has completed with no errors"
exit

Thanks!

---------- Post updated at 03:45 PM ---------- Previous update was at 03:22 PM ----------

The more I look at it, I think I need to add an 'else' section to capture error in return code under the first command. Is that correct?

Like such:

Code:
for M in dropLock createApplicationT enableConnects
do
        if [ $M != createApplicationT ]
        then
        echo "Executing MaxL: $M"
        . $essbasepath/startMaxl.sh $maxl_script_path/$M.mxl $essb_user $essb_pswd $essb_srvr $essb_app $essb_db $maxllog >> $logfile 
    
            _RC=$?
            if [ $_RC -eq 0 ]
            then
                echo "$M : Successful"
                continue
            else 
                echo "Encountered Error in MaxL: $M"
                echo "Exiting Shell Session"
                echo  | mail -s "Encountered Error in MaxL: $M" z@z.com
                exit 1
            fi
        fi
        
    . $essbasepath/startMaxl.sh $maxl_script_path/$M.mxl  $essb_user $essb_pswd $essb_srvr $essb_temp_app $essb_app $essb_db $maxllog >> $logfile 
        
        _RC=$?
        if [ $_RC -eq 0 ]
        then
            echo "$M : Successful"
            continue
        fi
    
    echo "Encountered Error in MaxL: $M"
    echo "Exiting Shell Session"
    echo  | mail -s "Encountered Error in MaxL: $M"  z@z.com
    exit 1
done

echo "${_SN} has completed with no errors"
exit 0




Moderator's Comments:
Mod Comment For your security, I replaced the actual e-mail address with a dummy one.

Last edited by RudiC; 03-09-2017 at 06:00 PM..
# 5  
Old 03-09-2017
If the difference is just one command argument you can use a shell variable for it.
If empty it is translated to no argument. (BTW in this case it must not be put into "quotes". "" would make an empty argument.)
Code:
for M in dropLock createApplicationT enableConnects
do
  echo "Executing MaxL: $M"
  if [ $M != createApplicationT ]
  then
    extra=""
  else
    extra=$essb_temp_app
  fi  
  . $essbasepath/startMaxl.sh $maxl_script_path/$M.mxl  $essb_user $essb_pswd $essb_srvr $extra $essb_app $essb_db $maxllog >> $logfile    
  if [ $? -ne 0 ]
  then
    echo "Encountered Error in MaxL: $M"
    echo "Exiting Shell Session"
    echo  | mail -s "Encountered Error in MaxL: $M" z@z.com
    exit 1
  fi
  echo "$M : Successful"
done

echo "${_SN} has completed with no errors"
exit

# 6  
Old 03-09-2017
If you have full access to the startMaxl.sh script, consider making the extra parameter the last one - that would make parameter handling way easier as every parameter would have the same position.
# 7  
Old 03-10-2017
This is incredible - thank you all for your help!

I will update the *.mxl to ensure the "outlyer" parameter comes last.

Thanks!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Emergency UNIX and Linux Support

For loop exit

Below for loop not exiting. Can someone help? JBOSS_INST_ARGS=01 02 if ; then for i in $JBOSS_INST_ARGS; do /u/jboss-6.1.0.Final/bin/jboss_init_wise$i.sh start; done (8 Replies)
Discussion started by: vino_hymi
8 Replies

2. Shell Programming and Scripting

Strange exit of while loop

This code is used to check for duplicate ip and hostnames in an /etc/hosts file CENTRAL is path to /etc/hosts AWK =awk #check CENTRAL for duplicate ips or hostnames# grep -v "^#" $CENTRAL | $AWK '{ print $1, $2; }' | \ while read ip hostname do if... (5 Replies)
Discussion started by: trimike
5 Replies

3. Shell Programming and Scripting

Loop exit control

Hi I would like to exit the loop below on <Enter> even if it sleeps. Is it possible? while true do my_procedure; sleep 60 done Thanks (7 Replies)
Discussion started by: zam
7 Replies

4. Shell Programming and Scripting

Exit from loop

hi, how to exit from "if" loop?actually i have mutliple "if" conditions, i have to exit from each "if" loop,if it is true...:confused: Please suggest me... (3 Replies)
Discussion started by: sreelu
3 Replies

5. Shell Programming and Scripting

Exit from loop only when selected

I am trying to get my program to exit when the answer to my question is positive, if I am asking if the answers are correct in the entries that the user inputted and the user says no how do I then have it exit? If they say everything is correct then it continue into the program, I think I am close... (2 Replies)
Discussion started by: gumbi17
2 Replies

6. Shell Programming and Scripting

exit out of a while loop with error

im running a while loop as a file watcher, with incremental counter on the retries..however when the retries reach it's limit i want it exit and echo and error and stop the batch. Im not sure the code i have will do that already... Here is what i have that works: #!/usr/bin/ksh count=0... (2 Replies)
Discussion started by: sigh2010
2 Replies

7. Shell Programming and Scripting

how to exit a while true loop

Hi guys, I'm new to unix but loving it!! BUT this is driving me nuts as i can't work out the best way to do it. I have a while true loop that i use to monitor something. For my own reasons in ths script i have disabled the CTRL C using the trap command. But i want to put in a option to exit... (5 Replies)
Discussion started by: Noob e
5 Replies

8. Shell Programming and Scripting

Method to exit a for loop

Hi All, Can someone let me know how i can exit a for loop without exiting the script itself .... will the break statement work .... please help .... -Regards (2 Replies)
Discussion started by: Rohini Vijay
2 Replies

9. Shell Programming and Scripting

while loop exit

i wrote a while script as part of a huge program. this script, once picked, begins to output data to the person using it. pretty easy, as the person doesn't have to keep typing commands to get the output that the while loop automatically throws out. now, the thing is, while this while-script... (3 Replies)
Discussion started by: Terrible
3 Replies

10. Shell Programming and Scripting

csh exit while loop on keystroke

#!/bin/csh I'm using a `while(1)` loop to dispaly real-time information about various files on my system, and I use ^C to exit it when needed. I was hoping there was a way to exit the script on a normal keystroke such as "q". Can someone point me in the right direction? I'm willing to use a... (7 Replies)
Discussion started by: seg
7 Replies
Login or Register to Ask a Question