Turn off exec 2>${_ERRORFILE} > ${_LOGFILE} feature?

Hi Folks -

To make a long story short, this script is loaded into a workbench, executed via workbench user interface, and then I need to display the output on screen. However, I'm using the functions to direct stdout and stderror to their respective directories.

Is there a way to turn that off so I can leverage the cat command? Else, when I place my cat commands just prior to the exits, it doesn't work.

Here is my script:

#!/bin/bash
#::------------------------------------------------------------------------
#::-- Script Name: FDMEE_Act_Load.sh
#::  
#::-- Description: This Script Executes the FDMEE RunBatch Utility
#::                
#::-- Paramaters:  Call setenv.sh to determine the following:
#::                 1. Login
#::                 2. Password
#::                 3. Server
#::                 4. Application
#::                 5. Database
#::                 6. etc
#::      
#::-- Author:        
#::-- Date:            
#:: ------------------------------------------------------------------------
source /app/hyp_app/scripts/setenv.sh

#::-- Set Script Name --::#
_SN=${0##*/}

echo Script Name: ${_SN}
#::-- Script Name w/o EXT: ${_SN%%.sh*} --::#

#::-- Set Log & Error Files --::#
_INTRA_PATH=${_FDMEE_LOG_DIR}
_ERROR_INTRA_PATH=${_FDMEE_ERROR_DIR}

_LOGFILE=${_INTRA_PATH}/${_DATETIMESTAMP}_${_SN%%.sh*}.log
_ERRORFILE=${_ERROR_INTRA_PATH}/${_DATETIMESTAMP}_${_SN%%.sh*}.err
    echo ${_LOGFILE}

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

#::-- Begin Script Processing --::#
echo ------------------------------------------
echo ${_SN} Starting at ${_TIME}                      
echo ------------------------------------------
echo ------------------------------------------
echo Execute FDMEE Actuals Load           
echo ------------------------------------------
 
. ${_FDMEE_UTIL_DIR}/runbatch.sh $APPID -f:${_FDMEE_APP_PSWRD} ${_FDMEE_BATCH}

if [ $? -eq 0 ]
then
    echo ------------------------------------------
    echo ${_SN%%.sh*} - Completed Successfully           
    echo ------------------------------------------
    echo ------------------------------------------
    echo Script Process Exiting Normally          
    echo ------------------------------------------
  exit 0
else
    echo ------------------------------------------
    echo ${_SN%%.sh*} - Completed Unsuccessfully           
    echo ------------------------------------------
    echo ------------------------------------------
    echo Script Process Exiting Abnormally          
    echo ------------------------------------------
  exit 1
fi

Thank you!

Hi, try:

cat something > /dev/tty

Hi there -

Thanks for the reply. Unfortunately, that didn't work.

Hi,

The trick is to assign additional file descriptors to stdout and stderr before you re-direct them, then point them back to the additional re-directors you 'saved' earlier, so to speak.

For example:

$ cat script.sh
#!/bin/bash

exec 3>&1 4>&2 1>out 2>err
echo "This is standard output from the script"
(>&2 echo "And here is an error")
exec 1>&3 2>&4
echo "And here is standard output as normal again"
(>&2 echo "And stderr too")

$ cat out
$ cat err
$ ./script.sh
And here is standard output as normal again
And stderr too
$ cat out
This is standard output from the script
$ cat err
And here is an error
$

Hope this helps.

1 Like

What did not work? What did you do?

I thought you wanted the cat command just before the exit command?

To show the principle if you execute this script

exec > logfile 2>errfile
echo some error >&2
echo some message
cat logfile > /dev/tty

The screen should display "some message"
The file logfile should contain "some message"
The file err file should contain "some error"

Does that not happen?

Hi Folks -

Thanks for all your help!!

This is what did thw trick for me, thanks to drysdalk!

source /app/hyp_app/scripts/setenv.sh

#::-- Set Script Name --::#
_SN=${0##*/}

echo Script Name: ${_SN}
#::-- Script Name w/o EXT: ${_SN%%.sh*} --::#

#::-- Set Log & Error Files --::#
_INTRA_PATH=${_FDMEE_LOG_DIR}
_ERROR_INTRA_PATH=${_FDMEE_ERROR_DIR}

_LOGFILE=${_INTRA_PATH}/${_DATETIMESTAMP}_${_SN%%.sh*}.log
_ERRORFILE=${_ERROR_INTRA_PATH}/${_DATETIMESTAMP}_${_SN%%.sh*}.err

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

exec 3>&1 4>&2 1>${_LOGFILE} 2>${_ERRORFILE}

#::-- Begin Script Processing --::#
echo ------------------------------------------
echo ${_SN} Starting at ${_TIME}                      
echo ------------------------------------------
echo ------------------------------------------
echo Execute FDMEE Actuals Load           
echo ------------------------------------------
 
. ${_FDMEE_UTIL_DIR}/runbatch.sh $APPID -f:${_FDMEE_APP_PSWRD} ${_FDMEE_BATCH}

if [ $? -eq 0 ]
then
    echo ------------------------------------------
    echo ${_SN%%.sh*} - Completed Successfully           
    echo ------------------------------------------
    echo ------------------------------------------
    echo Script Process Exiting Normally          
    echo ------------------------------------------
    exec 1>&3 2>&4
    cat ${_LOGFILE}
  exit 0
else
    echo ------------------------------------------
    echo ${_SN%%.sh*} - Completed Unsuccessfully           
    echo ------------------------------------------
    echo ------------------------------------------
    echo Script Process Exiting Abnormally          
    echo ------------------------------------------
  exit 1
fi

Thanks again!