Trap explanation?

Hi Folks -

Could someone help me understand something about my trap statement? I've been using the following trap statement for a while now and its works perfectly. The first part of the shell scripts look like such:

source "/home/EPMTrain1/Hyperion_Batch/Scripts/Batch/_env.sh"

#::-- Set Log & Error subdirectories pertaining to the specific process --::#
_PLOGPATH=Auto_Train_Logs/
_PERRORPATH=Auto_Train_Errors/

#::-- Establish STDOUT and STDERROR repositories --::
_INTRAPATH=${_LOGPATH}${_PLOGPATH}${_YEAR}_${_MONTH}${_DAY}
_ERRORINTRAPATH=${_ERRORPATH}${_PERRORPATH}${_YEAR}_${_MONTH}${_DAY}
    
mkdir -p "${_INTRAPATH}"
mkdir -p "${_ERRORINTRAPATH}"

#::-- Prepare File Name Format --::#
#::   _SN          = Script Name with extension
#::   ${_SN%%.sh*} = Script name without extension
#::   _FN          = File Name

_SN=${0##*/}
_FN=${_DATESTAMP}_${_TIME}_${_SN%%.sh*}

#::-- Establish STDOUT and STDERROR files --::#
_LOGFILE=${_INTRAPATH}/${_FN}.log
_ERRORFILE=${_ERRORINTRAPATH}/${_FN}.err
_MAXLLOGFILE=${_INTRAPATH}/${_FN}_MAXL.log

#::-- Direct STDOUT and STDERROR to repositories --::# 
exec 2>"${_ERRORFILE}" > "${_LOGFILE}"

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

My question is, in the trap argument, if ${_ERRORFILE} doesn't exist, it will proceed to the next command in the argument. but if it doesn't exist, why would I need to "remove it" again? Obviously it will always result in a "0" of that command since I"m forcing it (and thus, will proceed due to &&) but I'm just curious.

Perhaps someone can explain it so it makes more sense to me? Thanks!

Hi,

test -s checks if the file exists and its size is greater than zero.
So if the file exists but is empty it will be removed.

But if the file doesn't exist, that condition fails then thus, will execute the command on the right side of the ||. Therefore, why is a rm -f necessary here?

---------- Post updated at 07:58 AM ---------- Previous update was at 07:52 AM ----------

Perhaps in case the file is empty? Ah i see.

Of course, this only runs at the end of the script (i.e. the EXIT point of the shell)

Is there anything else in the script or is this the last operation? If it is, then I would get rid of the trap altogether or put it up at the top so it cleans up if your script aborts somewhere.

Robin

Hi Robin -

Thanks for the note! I'm sorry I wasn't clear - this is at the top of my script. My former code snippet was just a portion.

Here is my full script:

#!/bin/bash
#::--:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#
#::-- Script Name: EPMTrain1.sh                                               --::#
#::                                                                           --::#
#::-- Description: Allows functionality to Login to Essbase                   --::#
#::                                                                              --::#
#::                                                                              --::#
#::                                                                              --::#
#::--  Calls:      _env.sh                                                    --::#
#::--  Called By:  N/A                                                        --::#
#::                                                                              --::#
#::-- Parameters:  Not Applicable                                               --::#
#::                                                                              --::#
#::-- Author:      Name                                                     --::#
#::-- Date:                                                                       --::#
#::                                                                              --::#
#::--:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#

source "/home/EPMTrain1/Hyperion_Batch/Scripts/Batch/_env.sh"

#::-- Set Log & Error subdirectories pertaining to the specific process --::#
_PLOGPATH=Auto_Train_Logs/
_PERRORPATH=Auto_Train_Errors/

#::-- Establish STDOUT and STDERROR repositories --::
_INTRAPATH=${_LOGPATH}${_PLOGPATH}${_YEAR}_${_MONTH}${_DAY}
_ERRORINTRAPATH=${_ERRORPATH}${_PERRORPATH}${_YEAR}_${_MONTH}${_DAY}
    
mkdir -p "${_INTRAPATH}"
mkdir -p "${_ERRORINTRAPATH}"

#::-- Prepare File Name Format --::#
#::   _SN          = Script Name with extension
#::   ${_SN%%.sh*} = Script name without extension
#::   _FN          = File Name

_SN=${0##*/}
_FN=${_DATESTAMP}_${_TIME}_${_SN%%.sh*}

#::-- Establish STDOUT and STDERROR files --::#
_LOGFILE=${_INTRAPATH}/${_FN}.log
_ERRORFILE=${_ERRORINTRAPATH}/${_FN}.err
_MAXLLOGFILE=${_INTRAPATH}/${_FN}_MAXL.log

#::-- Direct STDOUT and STDERROR to repositories --::# 
exec 2>"${_ERRORFILE}" > "${_LOGFILE}"

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

#::-- Begin Script Processing --::#
echo ---------------------------------------------
echo "${_SN} beginning at ${_TIME}"                           
echo ---------------------------------------------
echo ---------------------------------------------                                                                                            
echo "Check for flag file"                                         
echo ---------------------------------------------

_FLAGFILE=${_FLAGFILEPATH}EPMTrain1.txt

if [ -e "${_FLAGFILE}" ]
then
    echo
    echo "Flag file detected"
    echo "Unable to proceed - exiting shell session"
    echo
    sleep 10
    exit 0
else
    echo
    echo "Flag file not detected - now establishing flag file"
    echo "Proceeding with script execution"
    echo
    echo "FlagFile">>"${_FLAGFILE}"
fi

echo ---------------------------------------------------------                                                                                                
echo "Login to ${_ESSB_SRVR}"                                         
echo ---------------------------------------------------------

. "${_STARTMAXLPATH}startMaxl.sh" "${_MAXLSCRIPTPATH}AC.mxl" ${_ESSB_USER} ${_ESSB_PSWD} ${_ESSB_SRVR} ${_MAXLLOGFILE}
_RVAL=$?

if [ $_RVAL -eq 0 ]
then
  echo ---------------------------------------------------------
  echo "Login to ${_ESSB_SRVR} : Successful"                           
  echo ---------------------------------------------------------
  
else
  echo ---------------------------------------------------------
  echo "Login to ${_ESSB_SRVR} : Unsuccessful"                      
  echo ---------------------------------------------------------
  exit 1
  fi

echo ---------------------------------------------------------                                                                                                
echo "Delete flag file"                                         
echo ---------------------------------------------------------
echo

rm -f "${_FLAGFILE}" && echo "Flag file deleted" || echo "Flag file did not exist"

echo
echo ---------------------------------------------------------                                                                                                
echo "${_SN} - Completed Successfully"
echo
echo "Normal Exit"                                        
echo ---------------------------------------------------------
exit 0

That looks almost char-for-char identical to code I wrote a few months ago. It began when simplifying their code with this:

exec 2>"${_ERRORFILE}" > "${_LOGFILE}"

They were thrilled they didn't have to do echo ... >> logfile and command 2>>errorlogfile every single time, but they didn't like that _LOGFILE was always created even when no errors occurred. So I added the trap to clean up the log file at exit unless it had greater-than-zero size.

Corona -

Yes that was me :slight_smile: It's been working fantastically!!!!

I was just having trouble understanding it for some reason, that's all.

Thank you!