I used to be a contractor at a huge facility that processes credit card
transactions. This Korn shell script has been running there every 5
minutes, 24/7 for years and years now. Good example of how to do some
pretty fancy stuff:
#
#
#This script loads all of the staging tables in the Oracle staging
tablespace with the data downloaded
#from the mainframe via Connect Direct. It also loads the verification
file associated with the
#table being loaded into the LOAD_VERIFY_TRIGGER_TABLE. The script is
initiated with Connect Direct, which
#passes arguments to the script via the command line. The two arguments
passed to this script are the
#staging table name, table($1), and the platform, plat($2). These
arguments(variables) are passed to the
#script as positional variables. This script only loads the
verification file if a .log file was created
#and a .bad file was not created when SQL*Loader was initiated to load
the data file. This script also
#archives the data & verification files to the /stage/archive directory
after successful loads. The script
#copies the load_verify_trigger_table control file to the
/ldrscripts/temp directory with a meaningful name
#so that all .log and .bad files will have different names. All
messages resulting from this script are
#written to the daily_load.log file.
#
export ORACLE_HOME=/u01/app/oracle/product/8.0.4
export ORACLE_SID=spsi1d
export ORACLE_TERM=vt100
export PATH=$ORACLE_HOME/bin:/usr/bin:/etc:/usr/sbin:/usr/ucb:/bin:
#
#Move the table name and platform supplied as command line variables
through Connect Direct into
#user defined variables within the script
#
table=$1
plat=$2
#
#Get the julian date to use as a file extension when the data file and
verification file are
#archived after successful loads
#
jdate=`date '+%j'`
timestamp=`date '+%H%M'`
#
#Get the date in standard date format to use as the beginning of each
message sent to daily_load.log
#
ndate=`date`
#
#Ensure that the data file and verification file exist on the Unix box
prior to initiating the shell
#processing and SQL*Loader. If either file does not exist, send error
messages to the daily_load.log and exit
#the script. This is an if-then as opposed to an if-then-else test,
which is why the [-ne] test is used.
#
test -f ${table}_${plat}.dat
if [ $? -ne 0 ]
then
echo "${ndate} -- Error-Data file ${table}_${plat}.dat was NOT
downloaded correctly" >>daily_load.log
echo "${ndate} -- Error-Data file ${table}_${plat}.dat was NOT
downloaded correctly~${table}_${plat}" >>daily_load_${table}_${plat}.log
sqlldr stage/that_stage_thing
control=ldrscripts/sqlload_messages.ctl data=daily_load_${table}_${plat}.log
rm daily_load_${table}_${plat}.log
exit
fi
test -f ${table}_ver_${plat}.dat
if [ $? -ne 0 ]
then
echo "${ndate} -- Error-Verification file ${table}_ver_${plat}.dat
was NOT downloaded correctly" >>daily_load.log
echo "${ndate} -- Error-Verification file ${table}_ver_${plat}.dat
was NOT downloaded correctly~LOAD_VERIFY_TRIGGER_TABLE"
>>daily_load_${table}_${plat}.log
sqlldr stage/that_stage_thing
control=ldrscripts/sqlload_messages.ctl data=daily_load_${table}_${plat}.log
rm daily_load_${table}_${plat}.log
exit
fi
#
#Remove all .log and .bad files for this table that are left from the
last script execution.
#
if test -f ${table}_${plat}.log
then
rm ${table}_${plat}.log
fi
if test -f ${table}_ver_${plat}.log
then
rm ${table}_ver_${plat}.log
fi
if test -f ldrscripts/${table}_${plat}.bad
then
rm ldrscripts/${table}_${plat}.bad
fi
if test -f ldrscripts/temp/${table}_ver_${plat}.bad
then
rm ldrscripts/temp/${table}_ver_${plat}.bad
fi
#
# If the merchant reference table is being downloaded, performance a diff
# command to produce a file of changes from the previous download file.
#
if test $table = "merchant_ref"
then
if ls archive/${table}_${plat}.*.* > /dev/null 2>&1
then
#
# Get the most recent merchant reference download and performance diff.
#
ls -t archive/${table}_${plat}.*.* | read line
prevfile=`echo $line`
diff ${prevfile} ${table}_${plat}.dat | grep "> " | sed s/^'> '//
>${table}_${plat}_diff.dat
sqlldr stage/that_stage_thing
control=ldrscripts/${table}_${plat}.ctl data=${table}_${plat}_diff.dat
log=${table}_${plat}.log bad=${table}_${plat}.bad
# mv -f ${table}_${plat}_diff.dat
archive/${table}_${plat}_diff.${timestamp}.${jdate}
else
#
# If a previous download doesn't exist, load current file.
#
sqlldr stage/that_stage_thing
control=ldrscripts/${table}_${plat}.ctl data=${table}_${plat}.dat
fi
else
#
#Load the data file into the staging tablespace with its corresponding
control file using SQL*Loader
#
sqlldr stage/that_stage_thing
control=ldrscripts/${table}_${plat}.ctl data=${table}_${plat}.dat
fi
#
#If the .log file exists, SQL*Loader executed and ATTEMPTED to load the
data, else send an error
#to the daily_load.log for the data file
#
if test -f ${table}_${plat}.log
then
#
#If the .bad file exist, one or more of the records did not load, which
is processed in this
#script as an error written to daily_load.log. If it does not exist,
this script ASSUMES that
#the data loaded, sends a message indicating this to the daily_load.log
and executes the
#verification file load.
#
if test -f ldrscripts/${table}_${plat}.bad
then
echo "${ndate} -- Error loading the data file
${table}_${plat}.dat" >>daily_load.log
echo "${ndate} -- Error loading the data file
${table}_${plat}.dat~${table}_${plat}" >>daily_load_${table}_${plat}.log
sqlldr stage/that_stage_thing
control=ldrscripts/sqlload_messages.ctl data=daily_load_${table}_${plat}.log
rm daily_load_${table}_${plat}.log
exit
else
echo "${ndate} -- Data file ${table}_${plat}.dat was loaded
successfully" >>daily_load.log
echo "${ndate} -- Data file ${table}_${plat}.dat was loaded
successfully~${table}_${plat}" >>daily_load_${table}_${plat}.log
sqlldr stage/that_stage_thing
control=ldrscripts/sqlload_messages.ctl data=daily_load_${table}_${plat}.log
rm daily_load_${table}_${plat}.log
#
#Prior to loading the verification file, the static verif. table .ctl file
#is copied/renamed to the temp directory for use in loading the
verification file. This is done so that
#the .log and .bad files created from the load will have a meaningful
name that relates to the .ctl file.
#
cp -f ldrscripts/load_verify_trigger_table.ctl
ldrscripts/temp/${table}_ver_${plat}.ctl
sqlldr stage/that_stage_thing
control=ldrscripts/temp/${table}_ver_${plat}.ctl
data=${table}_ver_${plat}.dat
#
#If the .log file exists, SQL*Loader executed and ATTEMPTED to load the
verif. record, else send
#an error to the daily_load.log
#
if test -f ${table}_ver_${plat}.log
then
#
#If the .bad file exist, one or more of the records did not load, which
is processed in this
#script as an error written to daily_load.log. If it does not exist,
this script ASSUMES that
#the data loaded and sends a message indicating this to the daily_load.log
#
if test -f ldrscripts/temp/${table}_ver_${plat}.bad
then
echo "${ndate} -- Error loading the verification file
${table}_ver_${plat}.dat" >>daily_load.log
echo "${ndate} -- Error loading the verification file
${table}_ver_${plat}.dat~LOAD_VERIFY_TRIGGER_TABLE"
>>daily_load_${table}_${plat}.log
sqlldr stage/that_stage_thing
control=ldrscripts/sqlload_messages.ctl data=daily_load_${table}_${plat}.log
rm daily_load_${table}_${plat}.log
exit
else
echo "${ndate} -- Verification file
${table}_ver_${plat}.dat was loaded successfully" >>daily_load.log
echo "${ndate} -- Verification file
${table}_ver_${plat}.dat was loaded
successfully~LOAD_VERIFY_TRIGGER_TABLE" >>daily_load_${table}_${plat}.log
sqlldr stage/that_stage_thing
control=ldrscripts/sqlload_messages.ctl data=daily_load_${table}_${plat}.log
rm daily_load_${table}_${plat}.log
#
#The data and verification files are archived after a successful load
and the temporary .ctl file created
#specifically for this table's verification file is removed
#
mv -f ${table}_${plat}.dat
archive/${table}_${plat}.${timestamp}.${jdate}
mv -f ${table}_ver_${plat}.dat
archive/${table}_ver_${plat}.${timestamp}.${jdate}
rm ldrscripts/temp/${table}_ver_${plat}.ctl
fi
else
echo "${ndate} -- Error loading the verification file
${table}_ver_${plat}.dat" >>daily_load.log
echo "${ndate} -- Error loading the verification file
${table}_ver_${plat}.dat~LOAD_VERIFY_TRIGGER_TABLE"
>>daily_load_${table}_${plat}.log
sqlldr stage/that_stage_thing
control=ldrscripts/sqlload_messages.ctl data=daily_load_${table}_${plat}.log
rm daily_load_${table}_${plat}.log
exit
fi
fi
else
echo "${ndate} -- Error loading the data file ${table}_${plat}.dat"
>>daily_load.log
echo "${ndate} -- Error loading the data file
${table}_${plat}.dat~${table}_${plat}" >>daily_load_${table}_${plat}.log
sqlldr stage/that_stage_thing
control=ldrscripts/sqlload_messages.ctl data=daily_load_${table}_${plat}.log
rm daily_load_${table}_${plat}.log
exit
fi