Exec command with mutt - turn on & off?

Hi Folks -

Quick question around the exec command again.

At the end of my script, I check for specific error codes that are returned from a process I execute within the shell script. Based on the error code, I send an email.

Do I need to turn off exec feature prior to send each email so the piping will work correctly? I would have tested this out before posting but i dont have that ability right now. Thank you all!

Here is my script:

#!/bin/bash
#:: ------------------------------------------------------------------------
#::-- Script Name: Data_Export.sh
#::  
#::-- Description: This script leverages startMaxl.sh to execute
#::                an All Data Essbase export
#::                  
#:: 
#::-- Parameters:  Call properties file to get environment variables to determine 
#::                login info, database, application, etc.
#::        
#::-- Author:       
#::-- Date:           
#:: ------------------------------------------------------------------------

#source /u01/Hyperion_Batch/Scripts/Batch/_env.sh

#::-- Set Script Action --::#

_ACTION=Export

#::-- Set Script Name --::#
#::-- ${_SN%%.sh*} --::

_SN=${0##*/}

#::-- Set Path Variables --::

cd $HOME

_MAINPATH=$(pwd)/Hyperion_Batch/
_LOGPATH=Logs/
_ERRORPATH=Errors/
_SCRIPTPATH=Scripts/
_MAXLPATH=Scripts/MaxL/
_FILEPATH=Files/
_EXPORTPATH=Exports/

#::-- Set Log & Error subdirectories pertaining to the specific process --::#

_PLOGPATH=Data_${_ACTION}_Logs/
_PERRORPATH=Data_${_ACTION}_Errors/

#::-- Set Date and Time Variable --::#
_DAY=$(date +%d)
_MONTH=$(date +%m)
_YEAR=$(date +%Y)
_DATESTAMP=${_YEAR}${_MONTH}${_DAY}
_HOUR=$(date +%H)
_MINUTE=$(date +%M)
_SECOND=$(date +%S)
_TIME=${_HOUR}${_MINUTE}
_DATETIMESTAMP=${_DATESTAMP}_${_TIME}

#::-- Establish STDOUT and STDERROR repositories --::
_ARC_LP=${_MAINPATH}${_LOGPATH}${_PLOGPATH}${_YEAR}_${_MONTH}${_DAY}
_ARC_EP=${_MAINPATH}${_ERRORPATH}${_PERRORPATH}${_YEAR}_${_MONTH}${_DAY}
    
mkdir -p ${_ARC_LP}
mkdir -p ${_ARC_EP}

#::-- Prepare File Name Format --::#
_HOST=$(hostname -f)
_FN=${_SN%%.sh*}_${_HOST}_${_TIME}

#::-- Establish STDOUT and STDERROR files --::#
_LF=${_ARC_LP}/${_FN}.log
_EF=${_ARC_EP}/${_FN}.err
_MLF=${_ARC_LP}/${_FN}_MAXL.log

#::-- Direct STDOUT and STDERROR to repositories --::# 
exec 2>${_EF} > ${_LF}

#::-- If empty, delete YYYY_MMDD error file subdirectory --::
trap "[ -s ${_EF} ] || rm -f ${_EF} && rmdir ${_ARC_EP}" EXIT

#::-- Set MaxL Specific Variables --::#

_ESSB_USER=admin
_ESSB_PSWD=welcome1
_ESSB_SRVR=exalytics-madc-01.zzz.lan
_ESSB_APP=AUTODEMO
_ESSB_DB=FIN_PLAN

_DATA_EXPORTPATH=${_MAINPATH}${_FILEPATH}${_EXPORTPATH}
_EXPORTTYPE=ALL_DATA

_STARTMAXL=/u02/EssbaseServer/essbaseserver1/bin/startMaxl.sh

#::-- Export Script Variables --::#

export _MAINPATH _LOGPATH _ERRORPATH _SCRIPTPATH _MAXLPATH _FILEPATH _EXPORTPATH _PLOGPATH _PERRORPATH
export _DAY _MONTH _YEAR _DATESTAMP _HOUR _MINUTE _SECOND _TIME _DATETIMESTAMP
export _HOST _FN _LF _EF _MLF
export _ESSB_USER  _ESSB_PSWD _ESSB_SRVR _ESSB_APP _ESSB_DB _DATA_EXPORTPATH _EXPORTTYPE _STARTMAXL

#:: Begin Script Processing --::#
echo ---------------------------------------------------------
echo ${_SN} beginning processing at ${_TIME}                          
echo                                                                                                     
echo Execute All Data ${_ACTION} against ${_ESSB_APP}                                         
echo ---------------------------------------------------------

. ${_STARTMAXL} ${_MAINPATH}${_MAXLPATH}All_Data_Export.mxl ${_ESSB_USER} ${_ESSB_PSWD} ${_ESSB_SRVR} ${_ESSB_APP} ${_ESSB_DB} ${_DATA_EXPORTPATH} ${_EXPORTTYPE} ${_YEAR}${_MONTH}${_DAY} ${_MLF}

if [ $? -eq 1 ]
then

    _TO=
    _CC=
    _BODY=
    _SUBJECT=
    _ATTACH=
    
    echo $_BODY|mutt -s "${_SUBJECT}" -a ${_ATTACH}  ${_TO} -c ${_CC}

  exit 0
  
elif [ $? -eq 2 ]
then

    _TO=
    _CC=
    _BODY=
    _SUBJECT=
    _ATTACH=
    
    echo $_BODY|mutt -s "${_SUBJECT}" -a ${_ATTACH}  ${_TO} -c ${_CC}
    
  exit 0
  
elif [ $? -eq 3 ]
then

    _TO=
    _CC=
    _BODY=
    _SUBJECT=
    _ATTACH=
    
    echo $_BODY|mutt -s "${_SUBJECT}" -a ${_ATTACH}  ${_TO} -c ${_CC}
    
  exit 0
  
else

    _TO=
    _CC=
    _BODY=
    _SUBJECT=
    _ATTACH=
    
    echo $_BODY|mutt -s "${_SUBJECT}" -a ${_ATTACH}  ${_TO} -c ${_CC}
    
  exit 0
  fi

I'm not sure why you're asking about exec , but there are definitely other problems in your script.

An exec command will not affect a pipeline whose first process is not reading anything from standard input and whose last process is not writing anything to standard output. If your pipeline writes to standard error, those messages will be written to the file specified by your exec command. If that isn't what you want to happen, you'll need to change something to make it do what you do want; but since you have given us no indication of what you want to happen, there is no way we can guess if your use of exec is right or wrong.

Expanding variables that have been set to empty strings (or to strings containing whitespace characters) without quoting them is a disaster waiting to happen. You have dozens of those.

Every time you execute a command (such as the command [ $? -eq x ] where x is some digit) resets the value of $? . So, only your first test of $? is testing the exit status of your dot ting the /u02/EssbaseServer/essbaseserver1/bin/startMaxl.sh command. Presumably, you need something more like:

. "${_STARTMAXL}" "${_MAINPATH}${_MAXLPATH}All_Data_Export.mxl" "${_ESSB_USER}" "${_ESSB_PSWD}" "${_ESSB_SRVR}" "${_ESSB_APP}" "${_ESSB_DB}" "${_DATA_EXPORTPATH}" "${_EXPORTTYPE}" "${_YEAR}${_MONTH}${_DAY}" "${_MLF}"
ret_val=$?

if [ $ret_val -eq 1 ]

and use $ret_val in all of your following tests instead of $? .

Thank you for the reply, Don.

So you're saying even if the first if conditional isn't met, it still resets '$?' ?

I'm also asking about exec because I have it set above:

#::-- Direct STDOUT and STDERROR to repositories --::#  exec 2>${_EF} > ${_LF}

Then I was wondering for instance if the following line in the first if conditional would just be sent to the error file directory instead of being executed:

    echo $_BODY|mutt -s "${_SUBJECT}" -a ${_ATTACH}  ${_TO} -c ${_CC}

As in other scripts, when using exec as I have set up, I need to 'turn it off' in order to use commands such a cat and so on.

Yes. A side effect of executing any pipeline of 1 or more commands is to set the value of the shell special parameter $? to the exit status of that pipeline.

Thanks, Don. If I may ask, can you explain that to me in a different way? I'm not quite following.

Thank you for your time.

Every command will (re)set the shell's $? parameter, so it can be meaningfully used to evaluate a certain command's success exactly ONCE. To evaluate it several times, assign it to a variable immediately after the command and use that variable hencefoth.

1 Like