Wednesday, 15 May 2013

Shell Script program ....

userpass=`echo $1 | cut -f2 -d '"'`
user=`echo $userpass | cut -f1 -d '/'`
passwd=`echo $userpass | cut -f2 -d '/'`
reqid=`echo $1 | cut -f2 -d " " | cut -f2 -d "="`
usrid=`echo $1 | cut -f4 -d " " | cut -f2 -d "="`
file=`echo $1 | cut -f9 -d " " | cut -f2 -d '"'`
p_outfile_path="$APPLCSF/$APPLOUT/o$reqid.out"
##
echo file1=$file
echo reqid=$reqid
echo usrid=$usrid
echo p_outfile_path=$p_outfile_path
################################
get_directory_name()
{
sqlplus -s $userpass <<EXIT
set serverout on size 100000;
set pages 0
set feed off
set verify off
declare
l_err   varchar2(100);
l_name  varchar2(100);
Begin
mas_pa_bs_reverse_bill.get_dir_name('$file_lookup',l_name,l_err);
if l_err is not null then
   dbms_output.put_line('TRAN_ERROR:'||l_err);
elsif l_name is null then
   dbms_output.put_line('Unable to get $file_type name');
else
   dbms_output.put_line(l_name);
end if;
end;
/
EXIT
}
################################
file_type='inbound directory'
file_lookup='ASSET_IN'
#echo "\nFind $file_type name :lookup=$file_lookup"
inbound_dir=`get_directory_name`
if  echo $inbound_dir |grep "TRAN_ERROR-" > /dev/null
then
  echo "\nError ... $inbound_dir "
  exit 1
fi
################################
file_type='inbound err directory'
file_lookup='ASSET_ERR'
inbound_err_dir=`get_directory_name`
if  echo $inbound_err_dir |grep "TRAN_ERROR-" > /dev/null
then
  echo "\nError ... $inbound_err_dir "
  exit 1
fi
#################################
file_type='inbound archive directory'
file_lookup='ASSET_ARCH'
inbound_arch_dir=`get_directory_name`
if  echo $inbound_arch_dir |grep "TRAN_ERROR-" > /dev/null
then
  echo "\nError ... $inbound_arch_dir "
  exit 1
fi
################################
file_type='inbound temp directory'
file_lookup='ASSET_TEMP'
inbound_temp_dir=`get_directory_name`
if  echo $inbound_temp_dir |grep "TRAN_ERROR-" > /dev/null
then
  echo "\nError ... $inbound_temp_dir "
  exit 1
fi
##############################
echo "Deleting files"

rm -f $inbound_temp_dir/sqlload_1 $inbound_temp_dir/sqlload_2
rm -f $MAS_TOP/bin/MAS_ASSET_BULK_UPDATE1.ctl
##

echo "Replace Strings reqid, usrid in Ctl file"

######################
##cp "$MAS_TOP/bin/MAS_ASSET_BULK_UPDATE.ctl" "$MAS_TOP/bin/MAS_ASSET_BULK_UPDATE1.ctl"
##chmod 777 "$MAS_TOP/bin/MAS_ASSET_BULK_UPDATE1.ctl"
##########################
####  Replace Strings reqid, usrid in Ctl file
##echo "Process: Replace Strings usrid, data_file_name with correct values in Ctl file"
##sed -e "s/v_file_name/$file/g" -e "s/v_user_id/$usrid/g" "$MAS_TOP/bin/MAS_ASSET_BULK_UPDATE.ctl" > "$MAS_TOP/bin/MAS_ASSET_BULK_UPDATE1.ctl"

##if [ $? -ne 0 ]
##then
##  {
 ## echo "Error: while replacing usrid with $usrid in Ctl file"
 ## exit 0
 ## }
##fi
inbound_dir=$inbound_dir

echo "inbound_dir $inbound_dir"
echo "inbound_arch_dir $inbound_arch_dir"
echo "inbound_temp_dir $inbound_temp_dir"
##
x=`date +"%m%d%y%H%M%S"`
##for file in `ls $inbound_dir`
##do
echo "\n directory looking: $inbound_dir"
echo "\n file name $file"
    echo "\n Processing file    : $file "
    file_name=`echo $file | cut -d"." -f1`
    #echo "file_name $file_name"

    if [ ! -f $inbound_dir/$file ]; then
        echo "\n $file not found $inbound_dir/$file"
        else
        echo "\n Datafile $file found "
        dos2unix $inbound_dir/$file t.txt
        mv t.txt $inbound_dir/$file
        echo "\n Running Sql loader for file $file"
        sqlldr $userpass control=$MAS_TOP/bin/MAS_ASSET_BULK_UPDATE.ctl data=$inbound_dir/$file log=$inbound_temp_dir/$file_name.log.$x bad=$inbound_temp_dir/$file_name.bad discard=$inbound_temp_dir/$file_name.dis  > $inbound_temp_dir/sqlload_1 > $inbound_temp_dir/sqlload_2
        echo "\n sqlload done, check for any errors"
        if grep -i error $inbound_temp_dir/sqlload_2 > /dev/null
        then
             echo "\n*****************************************************************************"
             echo "\nError in Loading the file to the staging table for the file $file "
             echo "\nError->$sqlload"
             echo "\n*****************************************************************************"
        elif [ -f $inbound_temp_dir/$file_name.err ]
        then
            echo "\n*****************************************************************************"
            echo "\n *******  Error while Loading to the staging table for the file $file, please see the error file ******* "
            echo "\n*****************************************************************************"           echo "\nMoving the file $file to the error directory "
            mv $inbound_temp_dir/$file_name.err $inbound_err_dir/$file_name.err.$x
        elif [ -f $inbound_temp_dir/$file_name.bad ]
        then
            echo "\n******************************************************************************"
            echo "\n *******  Error while Loading to the staging table for the file $file, please see bad file ******* "
            echo "\n*******************************************************************************"
            echo "\nMoving the file $file and bad file to the error directory "
            mv $inbound_temp_dir/$file_name.bad $inbound_err_dir/$file_name.bad.$x
       
        elif [ -f $inbound_temp_dir/$file_name.dis ]
        then
            echo "\n*******************************************************************************"
            echo "\n *******  Error while Loading to the staging table for the file $file, please see discard file ******* "
            echo "\n*******************************************************************************"
            echo "\nMoving the file $file and disracrd file to the error directory "
            mv $inbound_temp_dir/$file_name.dis $inbound_err_dir/$file_name.dis.$x
        fi
            echo "Successfully loaded records from datafile $file"
            cat $inbound_temp_dir/$file_name.log.$x |grep "Total logical"
            ####
            cat $inbound_temp_dir/$file_name.log.$x |grep "Total logical" >> $p_outfile_path
            ####
 
            echo "\n No bad file found for datafile $file"
            echo "\n No Discard file found for datafile $file"           
            echo "\n\n"
            echo "\nMoving data file $file to the Archive Directory"
                    mv $inbound_dir/$file $inbound_arch_dir/$file.$x

        fi
##done
#       
echo "Request : $reqid Completed">$inbound_temp_dir/$reqid.log
echo "\n Transaction Load Done">>$inbound_temp_dir/$reqid.log
echo "\n Please check attached Log for Details !!!">>$inbound_temp_dir/$reqid.log
reqidlog=`echo l$reqid.req`
echo "\n Request log is $reqidlog"

No comments:

Post a Comment