Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trap errors

Status
Not open for further replies.

ksbrace

Programmer
May 13, 2000
501
US
Hello,
I have a question regarding trap statements. Do they propagate up to the original shell script trap? For instance, I have a script which is separated into functions and those functions call some other script or execute something. Do I need to put a trap in each function or just the entry point? Do I need the extra trap in the cashier() method? Do I need to add a trap for each .shl script that is called in the nightly batch method? THanks in advance!! Below is part of my script to possibly help clarify question:
Code:
#!/bin/sh
########################################################
#
# bus_prod_night.sh
#
# 29-Jun-04     1.0     pH      Bourne-version based on bus_prod_night.shl
# 27-Jul-04     1.1     pH      explicit environmental settings
#
########################################################

########################################################
# time-date string functions
########################################################
# Monday, Tuesday, etc.
getDayName() {
   DAYNAME=`date +%A`
}

# January, February, etc.
getMonthName() {
   MONTHNAME=`date +%B`
}

# Sun=1, Mon=2, etc.
getWDay() {
    wday=`date +%w`
    DAY=`expr $wday + 1`
}

# HHMMSS format
getTime() {
    THE_TIME=`date +%H%M%S`
}

# DD-MON-YY format
getDate() {
    DATE=`date +%d-%b-%y | tr '[a-z]' '[A-Z]'`
}

# YYYYMMDD_HHMMSS format
getFileDate() {
   FileDate=`date +%Y%m%e_%H%M%S | tr ' ' '0'`
}

########################################################
# initializations
########################################################

HOSTN=`uname -n`
# uncomment this line to set test92 environment
# ORACLE_SID=test92; export ORACLE_SID

# replaced 7/27/04
# prod
# uncomment this line to set prod92 environment
ORACLE_SID=prod92; export ORACLE_SID

ORACLE_HOME=`/usr/local/bin/dbhome $ORACLE_SID`; export ORACLE_HOME
PATH=`echo $PATH | sed "s;$ORACLE_HOME/bin;;g"`:$ORACLE_HOME/bin; export PATH

cd $HOME

Message="The Prod Nightly Script ran at: $THE_TIME"
Subject="ProdNightly Time: $THE_TIME"
Recipient="me@me.com"
TrapRecipients="me@me.com"
# initialize date and time stamps
getTime
getDate
getFileDate
getWDay
getDayName
getMonthName

LogFile=$OW_LIS/bus_prod_night_${FileDate}.log

########################################################
#Trapping For Errors
########################################################
trap 'print ""; print "ERR exit status of some command was detected during the e
xecute of bus_prod_night.shl for ${ORACLE_SID} on ${HOST}."; print "Please revie
w the log file or screen output for details."; print ""; mailx -s "{HOSTN}-${ORACLE_SID}-ERR exit status detected in bus_prod_night.shl" ${TrapRecipients} < ${L
ogFile}; exit 1' ERR

########################################################
# logging function
########################################################
log() {
    getTime
    echo "$THE_TIME: $*" | tee -a $LogFile
}

########################################################
# test function
########################################################
tester() {
   log "STARTING tester"

   ls
   sleep 5
   echo MONTHNAME is $MONTHNAME

   log "ENDING tester"
}

########################################################
# daily batch functions
########################################################
cashier() {
trap 'print ""; print "ERR exit status of the cashier command was detected durin
g the execute of bus_prod_night.shl for ${ORACLE_SID} on ${HOST}."; print "Pleas
e review the log file or screen output for details."; print ""; mailx -s "{HOSTN
}-${ORACLE_SID}-ERR exit status detected in bus_prod_night.shl" ${TrapRecipients
} < ${LogFile}; exit 1' ERR

    log "BEGINNING TO EXECUTE cashier.sqr...."

    #Executing cashier.sqr script
    sqr $BANNER_LINKS/cashier.sqr / -f$OW_LIS/cashier_${FileDate}.lis > $OW_LIS/
cashier_${FileDate}.log

    #PRINT
    csprint $OW_LIS/cashier_${FileDate}.lis

    log "FINISHED EXECUTION of cashier.sqr"

} # cashier

#############################TBRYCSH SECTION#######################
tbrycsh() {

    log "BEGINNING TO EXECUTE TBRYCSH.sqr...."
    sqlplus / @$BANNER_LINKS/tbrycsh.sql F

    cp $HOME/tbrycsh.lis $OW_LIS/tbrycsh_${FileDate}.lis
    $RM -f $HOME/tbrycsh.lis

    if [ -f $OW_LIS/tbrycsh_${FileDate}.lis ]
       then
          #PRINT
          csprint $OW_LIS/tbrycsh_${FileDate}.lis
    else
       echo "NO OUTPUT FOR TBRYCSH..."
    fi

    log "FINISHED EXECUTION of TBRYCSH.sqr"

} # tbrycsh

########################################################
# all nightly batch procedures
########################################################
night_batch() {

    log "BEGINNING NIGHT$BATCH SECTION......."

    if [ -x $OW_MISC/shs_imm_hold_upd.shl ]; then
        log "Executing shs_imm_hold_upd.shl script..."
        $OW_MISC/shs_imm_hold_upd.shl shs_imm_hold_upd.dat
    fi

    if [ -x $OW_MISC/adm_dec7_upd.shl ]; then
       log "Executing adm_dec7_upd.shl script...."
       $OW_MISC/adm_dec7_upd.shl
    fi
    
    if [ -x $OW_MISC/adm_dec8_upd.shl ]; then
       log "Executing adm_dec8_upd.shl script...."
       $OW_MISC/adm_dec8_upd.shl
    fi

    if [ -x $OW_MISC/adm_birth_pin.shl ]; then
        log "Executing adm_birth_pin.shl script...."
        $OW_MISC/adm_birth_pin.shl
    fi

    if [ -x $OW_MISC/adm_declare_maj.shl adm_declare_maj.shl ]; then
        log "Executing adm_declare_maj.shl"
        $OW_MISC/adm_declare_maj.shl adm_declare_maj.dat
    fi

    log "FINISHED EXECUTING NIGHT$BATCH FILES....." 

} # night_batch
########################################################

########################################################
# all daily financial procedures
########################################################
daily_batch() {

#   uncomment these function calls to test

#   tester
   cashier
   tbrycsh
 
} # daily_batch

########################################################
# main procedure
########################################################
main() {
    log "###################################################"
    log "$DATE"
    log "$THE_TIME"
    log "###################################################"
 
# If script runs after midnight and before 8am mail a 
# notification to uhlge.   
   if [ $THE_TIME -gt "000000" -a $THE_TIME -lt "080000" ]
      then
         echo $Message | mailx -s "$Subject" "$Recipient" 
   fi

# If the time is after 8am and before 8pm output message
   if [ $THE_TIME -gt "080000" -a $THE_TIME -lt "200000" ]
      then
         echo "Prod Nightly does not get run during the day"
         echo "Prod Nightly attempted to run during the day $Message" | mailx -s
 "$Subject" "$Recipient"
   fi

#   uncomment to test functions
    daily_batch
    night_batch

} # main

main

# end bus_prod_night.sh

 
In your shell man page read carefully the discussion about traps.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
PHV,
I did read the man pages for trap BEFORE posting and it didn't explain my scenario. That was the purpose of this post to get an answer. I ended up testing out a scenario that I knew would throw an error and threw that into a script and had another script w/a trap call the script with the error. The caller caught the callee's error. Which was the answer that I was looking for, not "read the man pages".

 
ksbrace
I'm having the same problem but with a diff experience.

I am setting "trap "" 1 2 3" at the top of the main script. I am trying to stop users from doing a CTRL-C out of the script onto the command line.

The mainscript has a menu that calls other scripts, but the user can CTRL-C out of this called script.

How do I avoid this without having to set the trap in every script that is called from the main script?
 
You may change the intr character with stty on top of the mainscript, resetting it when exit.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top