TekiePrincess
Programmer
I am working on the following script. The original requirement was to just rename the files then move them to a seperate directory. I have the following as my base script and would like to get feedback on how I can make this better or any mistakes I may have overseen as a new shell scripter.
# Description : This script will rename and move files coming in from the 3PL to the appropriate directories then run the sql script to upload the data into the staging tables. Then run a concurrent program. Finally the files that were successful will be moved to an archive directory and those there weren't successful will be moved to an error directory.
# ========
# History
# ========
#
#**********************************************************************
# SDN file rename and move to appropriate directory for processing
#***Variables used throughout the shell script
#Set the environment
#ORACLE_HOME=$ORACLE_BASE/product/10.2.0
#CONNECTION=xxxxxxxxxxxx/xxxxxxxxxxxx@xxxxxx
#OWNER=xxxxx
#Script=xxxupload
#SCRIPT_PATH=`whence $0` >/dev/null 2>&1
#SCRIPT_HOME=`dirname $SCRIPT_PATH
#datadir="/u04/XXX/XXX_Interface/XXXFILES"
#archdir="path/archive"
#filename=${fn##*/}
#ORACLEID=xxxxx/xxxxxxx@xxxxx
#RespAppSN = SYSADMIN
#Respname='System Administrator'
#User_Name = XXXX
#[WAIT=N|Y|
#CONCURRENT = CONCURRENT
#ProgAppSN= FND
#ProgName= FNDSCURS
#PROGNAMDESC="Active Users"
#*********************************************************************
# Check Oracle Environment
#*********************************************************************
if [ -z "${ORACLE_HOME}" ]
then
echo ""
echo "ORACLE_HOME is not set."
echo "ORACLE_HOME is needed for SQL*Plus and SQL*LDR."
echo ""
echo "Exiting $0"
echo ""
exit 1
fi
if [ ! -f ${ORACLE_HOME}/bin/sqlplus ]
then
echo ""
echo "Can not find sqlplus."
echo ""
echo "Exiting $0"
echo ""
exit 1
fi
#*********************************************************************
# rename and move each file in upload directory
# move duplicate files (listed in $SUCCESS_LOG) to $ERROR_DIR
# upon completion move files to the archive directory
#*********************************************************************
NOW=$(date "+%Y%m%d_%H:%M")
#cd $datadir
for f in *.txt; do
fn="${f%.*}"
l=$(sed -n '3 {s/UserFilename="//;s/.txt"$//;p;q;}' "$f")
case $l in
SHIP_*_XXXX) mv "${fn##*/}.dat" "$datadir/XXXXX/$l.dat";;
PICK_*_XXXX) mv "${fn##*/}.dat" "$datadir/XXXXX/$l.dat";;
SHIP_*_XXXX) mv "${fn##*/}.dat" "$datadir/XXXX/$l.dat";;
PICK_*_XXXX) mv "${fn##*/}.dat" "$datadir/XXXX/$l.dat";;
LOCATOR_*) mv "${fn##*/}.dat" "$datadir/LOCATOR/$l.dat";;
*) echo "error, unknown file type: $l" > "$ERROR_LOG
continue
fi
filename=$l
#*********************************************************************
# Run sql*plus upload script for each file
#*********************************************************************
echo "Starting job..."
echo "Connecting to SQL*Plus..."
$ORACLE_HOME/bin/sqlplus -s <<EOF $CONNECTION
whenever sqlerror exit failure
exec $OWNER.$SCRIPT('$l');
echo $l $ROWCOUNT success
exit
EOF
#*********************************************************************
# Execute Concurrent Program for Oracle Applications
#*********************************************************************
# Concurrent Program will be called to upload the data into the staging tables
CONCSUB xxxxx/xxxxxxxx SYSADMIN 'System Administrator' xxx4 WAIT=Y CONCURRENT FND FNDSCURS PROGRAM_NAME='"Active Users"'
continue
#*********************************************************************
# move successfully uploaded files to $archdir
# move failed files to $ERROR_DIR
# update logs
#*********************************************************************
statcode=$?
if [ $statcode -eq 0 ] ; then
echo $NOW $FILE >> $SUCCESS_LOG
mv $FILE $SUCCESS_DIR/${FILE}_$NOW
else
echo $NOW $FILE sqlerror >> $ERROR_LOG
mv $FILE $ERROR_DIR/${FILE}_$NOW
fi
echo "SQL*Plus connection closing..."
echo "`date +%m%d%Y_%H:%M`: Done!"
echo
echo
done
# Description : This script will rename and move files coming in from the 3PL to the appropriate directories then run the sql script to upload the data into the staging tables. Then run a concurrent program. Finally the files that were successful will be moved to an archive directory and those there weren't successful will be moved to an error directory.
# ========
# History
# ========
#
#**********************************************************************
# SDN file rename and move to appropriate directory for processing
#***Variables used throughout the shell script
#Set the environment
#ORACLE_HOME=$ORACLE_BASE/product/10.2.0
#CONNECTION=xxxxxxxxxxxx/xxxxxxxxxxxx@xxxxxx
#OWNER=xxxxx
#Script=xxxupload
#SCRIPT_PATH=`whence $0` >/dev/null 2>&1
#SCRIPT_HOME=`dirname $SCRIPT_PATH
#datadir="/u04/XXX/XXX_Interface/XXXFILES"
#archdir="path/archive"
#filename=${fn##*/}
#ORACLEID=xxxxx/xxxxxxx@xxxxx
#RespAppSN = SYSADMIN
#Respname='System Administrator'
#User_Name = XXXX
#[WAIT=N|Y|
#CONCURRENT = CONCURRENT
#ProgAppSN= FND
#ProgName= FNDSCURS
#PROGNAMDESC="Active Users"
#*********************************************************************
# Check Oracle Environment
#*********************************************************************
if [ -z "${ORACLE_HOME}" ]
then
echo ""
echo "ORACLE_HOME is not set."
echo "ORACLE_HOME is needed for SQL*Plus and SQL*LDR."
echo ""
echo "Exiting $0"
echo ""
exit 1
fi
if [ ! -f ${ORACLE_HOME}/bin/sqlplus ]
then
echo ""
echo "Can not find sqlplus."
echo ""
echo "Exiting $0"
echo ""
exit 1
fi
#*********************************************************************
# rename and move each file in upload directory
# move duplicate files (listed in $SUCCESS_LOG) to $ERROR_DIR
# upon completion move files to the archive directory
#*********************************************************************
NOW=$(date "+%Y%m%d_%H:%M")
#cd $datadir
for f in *.txt; do
fn="${f%.*}"
l=$(sed -n '3 {s/UserFilename="//;s/.txt"$//;p;q;}' "$f")
case $l in
SHIP_*_XXXX) mv "${fn##*/}.dat" "$datadir/XXXXX/$l.dat";;
PICK_*_XXXX) mv "${fn##*/}.dat" "$datadir/XXXXX/$l.dat";;
SHIP_*_XXXX) mv "${fn##*/}.dat" "$datadir/XXXX/$l.dat";;
PICK_*_XXXX) mv "${fn##*/}.dat" "$datadir/XXXX/$l.dat";;
LOCATOR_*) mv "${fn##*/}.dat" "$datadir/LOCATOR/$l.dat";;
*) echo "error, unknown file type: $l" > "$ERROR_LOG
continue
fi
filename=$l
#*********************************************************************
# Run sql*plus upload script for each file
#*********************************************************************
echo "Starting job..."
echo "Connecting to SQL*Plus..."
$ORACLE_HOME/bin/sqlplus -s <<EOF $CONNECTION
whenever sqlerror exit failure
exec $OWNER.$SCRIPT('$l');
echo $l $ROWCOUNT success
exit
EOF
#*********************************************************************
# Execute Concurrent Program for Oracle Applications
#*********************************************************************
# Concurrent Program will be called to upload the data into the staging tables
CONCSUB xxxxx/xxxxxxxx SYSADMIN 'System Administrator' xxx4 WAIT=Y CONCURRENT FND FNDSCURS PROGRAM_NAME='"Active Users"'
continue
#*********************************************************************
# move successfully uploaded files to $archdir
# move failed files to $ERROR_DIR
# update logs
#*********************************************************************
statcode=$?
if [ $statcode -eq 0 ] ; then
echo $NOW $FILE >> $SUCCESS_LOG
mv $FILE $SUCCESS_DIR/${FILE}_$NOW
else
echo $NOW $FILE sqlerror >> $ERROR_LOG
mv $FILE $ERROR_DIR/${FILE}_$NOW
fi
echo "SQL*Plus connection closing..."
echo "`date +%m%d%Y_%H:%M`: Done!"
echo
echo
done