Trap command not working

Hi Folks -

For some reason, my trap command is not working. It's placed just prior to a normal exit:

#:: ------------------------------------------------------------------------
#::-- Script Name: LCM_Backup.sh
#::  
#::-- Description: This script leverages Utility.sh to perform LCM Backups
#:: 
#::-- Parameters:  Call properties file to get environment variables to determine 
#::                login info, database, application, etc.
#::        
#::-- Author:       Joe Shmoe
#::-- Date:           12/16/16
#:: ------------------------------------------------------------------------
#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/

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

_PLOGPATH=LCM_Logs/
_PERRORPATH=LCM_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

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

#::-- Establish LCM Variables --::

_EPM_SYSTEM_BIN=/u01/EPM/Oracle/Middleware/user_projects/epmsystem1/bin/
_IMPORTEXPORT_DIR=/u01/EPM/Oracle/Middleware/user_projects/epmsystem1/import_export/
_LCM_DIR=AUTODEMO_FIN_PLAN_CS/
_LCM_USER=admin
_LCM_PSWD=psswrd

#:: Begin Script Processing --::#
echo ---------------------------------------------------------
echo "${_SN} beginning at ${_TIME}"                        
echo                                                                                                        
echo "Execute ${_LCM_DIR} Life Cycle Management Backup"                                         
echo ---------------------------------------------------------

sed "s/name=\"\" password=\"\"/name=\"${_LCM_USER}\" password=\"${_LCM_PSWD}\"/" ${_IMPORTEXPORT_DIR}${_LCM_DIR}${_ACTION}.xml > ${_IMPORTEXPORT_DIR}${_LCM_DIR}${_ACTION}1.xml
rm ${_IMPORTEXPORT_DIR}${_LCM_DIR}${_ACTION}.xml
mv ${_IMPORTEXPORT_DIR}${_LCM_DIR}${_ACTION}1.xml ${_IMPORTEXPORT_DIR}${_LCM_DIR}${_ACTION}.xml

#:: Begin Script Processing --::
echo ---------------------------------------------------------                                                                                                                            
echo "Execute ${_LCM_DIR} Life Cycle Management Backup"                                         
echo ---------------------------------------------------------

sh ${_EPM_SYSTEM_BIN}Utility.sh ${_IMPORTEXPORT_DIR}${_LCM_DIR}${_ACTION}.xml

if [ $? -eq 0 ]
then
  echo ---------------------------------------------------------
  echo "${_LCM_DIR} Life Cycle Management Backup Successful"                           
  echo ---------------------------------------------------------
  #::-- If empty, delete YYYY_MMDD error file subdirectory --::
  trap "[ -s ${_EF} ] || rm -f ${_EF} ] && rmdir ${_ARC_EF}" exit 0
  
else
  echo ---------------------------------------------------------
  echo "${_LCM_DIR} Life Cycle Management Backup Unsuccessful"                      
  echo ---------------------------------------------------------
  exit 1
  fi

However, in earlier threads this months, I was told to place the trap earlier in my script. When I do so, I get an error with this message :

trap: usage: trap [-lp] [arg signal_spec ...]

Thanks!

Restore the 'exit 0' at the end of it back to EXIT. That part is not shell script -- it's a signal definition, and EXIT is a special value it also accepts (not technically a signal).

Place it early in your script, immediately after ${_EF} and ${_ARC_EF} are defined.

A primary use of trap is for interactive scripts - example: prevent users from typing ctrl/c to stop the process, because if the process gets interrupted in a bad place you have a problem.

It can also be used as a primary single known exit point when an error occurs.

trap "do something " EXIT

is how to trap an exit.

Your trap call is not like anything above AFAICS. What EXACTLY are you trying to do? - not how you want to do it. Please.

I recall that trap from a previous thread, where I wrote it. The objective is to remove the logfile and log folder on exit, if the log file is empty. It just had EXIT at the end of it at the time.

Then they went through a thread where they realized their 'exit 1's were causing nonzero exit statuses and must have edited that unrelated EXIT into 'exit 0' along with all the others to "fix" it.

Okay - the lack of EXIT triggered the request. Which still does not explain using the trap command inside an if block at the bottom - you pointed out that they had a correct version earlier.

Purely random changes to see what worked. OP knew where it really belonged and said so, but dumping it inside that block made the error go away "sometimes", i.e. whenever that block wasn't used.

You were told in that thread again and again how to use the trap command, and where to get basic information about it. I'm afraid it can't be made clearer.

Hi Folks -

Adjusted the code accordingly, however the directory is not being removed (Error file subdirectory).

#:: ------------------------------------------------------------------------
#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/

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

_PLOGPATH=LCM_Logs/
_PERRORPATH=LCM_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

#::-- 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_EF}" EXIT

#::-- Establish LCM Variables --::

_EPM_SYSTEM_BIN=/u01/EPM/Oracle/Middleware/user_projects/epmsystem1/bin/
_IMPORTEXPORT_DIR=/u01/EPM/Oracle/Middleware/user_projects/epmsystem1/import_export/
_LCM_DIR=AUTODEMO_FIN_PLAN_CS/
_LCM_USER=admin
_LCM_PSWD=welcome1

#:: Begin Script Processing --::#
echo ---------------------------------------------------------
echo "${_SN} beginning at ${_TIME}"                        
echo                                                                                                        
echo "Add credentials to ${_LCM_DIR}${_ACTION}.xml"                                         
echo ---------------------------------------------------------

sed "s/name=\"\" password=\"\"/name=\"${_LCM_USER}\" password=\"${_LCM_PSWD}\"/" ${_IMPORTEXPORT_DIR}${_LCM_DIR}${_ACTION}.xml > ${_IMPORTEXPORT_DIR}${_LCM_DIR}${_ACTION}1.xml
rm ${_IMPORTEXPORT_DIR}${_LCM_DIR}${_ACTION}.xml
mv ${_IMPORTEXPORT_DIR}${_LCM_DIR}${_ACTION}1.xml ${_IMPORTEXPORT_DIR}${_LCM_DIR}${_ACTION}.xml

if [ $? -eq 0 ]
then
  echo ---------------------------------------------------------
  echo "Credentials added to ${_LCM_DIR}${_ACTION}.xml Successful"                           
  echo ---------------------------------------------------------

else
  echo ---------------------------------------------------------
  echo "Credentials added to ${_LCM_DIR}${_ACTION}.xml Unsuccessful"                      
  echo ---------------------------------------------------------
  exit 1
  fi

  #:: Begin Script Processing --::
echo ---------------------------------------------------------                                                                                                                            
echo "Execute ${_LCM_DIR} Life Cycle Management Backup"                                         
echo ---------------------------------------------------------

sh ${_EPM_SYSTEM_BIN}Utility.sh ${_IMPORTEXPORT_DIR}${_LCM_DIR}${_ACTION}.xml

if [ $? -eq 0 ]
then
  echo ---------------------------------------------------------
  echo "${_LCM_DIR} Life Cycle Management Backup Successful"                           
  echo ---------------------------------------------------------
  exit 0
  
else
  echo ---------------------------------------------------------
  echo "${_LCM_DIR} Life Cycle Management Backup Unsuccessful"                      
  echo ---------------------------------------------------------
  exit 1
  fi

Could there be a hidden file in the directory that I"m not seeing?

Thanks!

Is the log file actually empty?

The log file will never be empty. A log file is always generated upon execution, but not an error file.

Therefore, my script will build the following directories to direct standard output and stadnard error:

<Path>/Logs/LCM_Logs/2016_1216
<Path>/Errors/LCM_Errors/2016_1216
```[/b]


If  ``` <Path>/Errors/LCM_Errors/2016_1216  ``` directory is empty \(no error files generated, then delete 2016_1216.

I hope I was clear enough?

But is the error log actually empty? Look at it with ls -l . Zero bytes?

_ARC_EF is not defined in your script. And, there's a ] too many in your trap statement.

1 Like

HI -

Yup, no error file is even generated.

---------- Post updated at 12:48 PM ---------- Previous update was at 12:45 PM ----------

Found the issue,

It should be

trap "[ -s ${_EF} ] || rm -f ${_EF} ] && rmdir ${_ARC_EP}" EXIT

I changed _ARC_EF to -ARC_EP. My fault! Thank you all for your help! Sorry for the bother!

You still have the extra ] in there, rm -f ${_EF} ] should be rm -f ${_EF}

Hi.

I often use set -o nounset near the top in non-trivial, non-demo scripts:

#!/usr/bin/env bash

# @(#) z3       Demonstrate set -o nounset

echo " Variable v1 is $v1"

set -o nounset
echo " Variable v1 is $v1"

which, when run, produces:

$ ./z3
 Variable v1 is 
./z3: line 8: v1: unbound variable

on a system like:

OS, ker|rel, machine: Linux, 3.16.0-4-amd64, x86_64
Distribution        : Debian 8.6 (jessie) 
bash GNU bash 4.3.30

Best wishes ... cheers, drl

1 Like

Hmm, it still works even with extra ]. Noted, though. Thank you!

It was spewing a ']: file not found' error into the file it was trying to delete.