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