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:

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!

Is correct, there is no problem.
You can also have

    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

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

That is fine, but, since an error occurred, I would use exit 1 for example, which exits with return code 1.

1 Like

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

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:

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:

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

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

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

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.

This is incredible - thank you all for your help!

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

Thanks!

Hi Simms,

my personal opinion regarding coding style is to not use exit in the middle of any program - at least not until the block of code is that short that the exit is not visible immediately - and I almost never use an exit within a function.

If it's just 10 lines coding style may be ignored all the way and I would say: Don't make a mountain out of a molehill.

stomp();