Tuesday, July 24, 2012

Shell Script example

Objectives:

  • Steps to Register Shell Script as a concurrent program
  • Sample Shell Script to copy the file from source to destination
  • Basic Shell Script Commands
Steps to Register Shell Script as a concurrent program

step 1:
=======
Place the <name>.prog script under the bin directory for your
applications top directory.

For example, call the script SAMPLE_DEMO.prog and place it under
$CUSTOM_TOP/bin


step 2:
=======
Make a symbolic link from your script to $FND_TOP/bin/fndcpesr
For example, if the script is called 
SAMPLE_DEMO.prog use this:
ln -s $FND_TOP/bin/fndcpesr SAMPLE_DEMO
This link should be named the same as your script without the
.prog extension.


Put the link for your script in the same directory where the
script is located.


step 3:
=======
Register the concurrent program, using an execution method of
‘Host’. Use the name of your script without the .prog extension
as the name of the executable.


For the example above:
Use 
SAMPLE_DEMO
step 4:
=======
Your script will be passed at least 4 parameters, from $1 to $4.

$1 = orauser/pwd
$2 = userid(apps)
$3 = username,
$4 = request_id

Any other parameters you define will be passed in as $5 and higher.
Make sure your script returns an exit status also.


Sample Shell Script to copy the file from source to destination

#Note: If you see # in front of any line it means that it’s a comment line not the actual code
#** ********************************************************************
# Created By : Mahammad S
# Creation Date : 13-FEB-2012
# Script Name : 
SAMPLE.prog
# Description : This Script accepts three parameters
# 1)Data File Name 2)Source Directory Path 3)Target Directory Path
# Then copy the file from source location to target location.
# If copy fails send the error status/message to concurrent program so that user can see status.
#
#
# ========
# History
# ========
# Version 1 Mahammad S 13-FEB-2012 Created for sample emps
#
#** ********************************************************************
#Parameters from 1 to 4 i.e $1 $2 $3 $4 are standard parameters
# $1 : username/password of the
database
# $2 : userid
# $3 : USERNAME
# $4 : Concurrent Request ID
DataFileName=$5
SourceDirectory=$6
TargetDirectory=$7
echo “————————————————–”
echo “Parameters received from concurrent program ..”
echo ” Time : “`date`
echo “————————————————–”
echo “Arguments : ”
echo ”
Data File Name : “${DataFileName}
echo ” SourceDirectory : “${SourceDirectory}
echo ” TargetDirectory : “${TargetDirectory}
echo “————————————————–”
echo ” Copying the file from source directory to target directory…”
cp ${SourceDirectory}/${DataFileName} ${TargetDirectory}
if [ $? -ne 0 ]
# the $? will contain the result of previously executed statement.
#It will be 0 if success and 1 if fail in many cases
# -ne represents not “equal to”

then
echo “Entered Exception”
exit 1
# exit 1 represents concurrent program status. 1 for error, 2 for warning 0 for success
else
echo “File Successfully copied from source to destination”
exit 0
fi
echo “****************************************************************”


Basic Shell Script Commands

# Create Directory
mkdir <dirname>
# Remove Directory
rmdir <dirname>
#remove folder with files
rm -r -f <dirname>
# Change Directory
cd <newpath>
# Create new file
vi <newfile.ext>
#insert data into file
vi <openfilename.ext>
esc i <make changes>
#Save file
esc :wq enter
# exit without saving changes
esc :q! enter
# open existing file
vi <existingfilename.ext>
#remove file
rm <filename.ext>
# copy file with same name
cp <sourcedir>/<sourcefilename.ext> <destinationdir>
# copy file with new name
cp <sourcedir>/<sourcefilename.ext> <destinationdir>/<newfilename.ext>
# Move file with same name
mv <sourcedir>/<sourcefilename.ext> <destinationdir>
# move file with data appended to filename in the front
mv <sourcedir>/<sourcefilename.ext> <destinationdir>/`date+%H%M%d%m%y`<filename.ext>
#print line
echo “your text here to print”
#print date
echo `date`



Shell script utilities



1. Submitting a concurrent program using Unix
One can code in the command as CONSUB within the shell script which can submit the concurrent program through shell script directly.
CONCSUB $ORACLEUSERID $RESPAPPLNAME $RESPNAME1
$USERNAME WAIT=N CONCURRENT $PROGAPPLNAME
$PROGSHORTNAME1 $PARAM1 $PARAM2 $PARAM3
Where
ORACLEUSERID – oracle apps userid/password. UserID/PWD
comes in parameter1
RESPAPPLNAME – Responsibility application name
RESPNAME1 – Responsibility name
USERNAME – Username who is submitting the program
WAIT- If needed waiting time can be added
PROGAPPLNAME- Actual program needs to be submitted
PROGSHORTNAME1- Concurrent program short name
PARAM1, PARAM2, PARAM3- Concurrent program Parameters

2. Compress the file in Unix

zip archivefile1 doc1 doc2 doc3
Finding the files in Unix
find . -name "*.*" -print | xargs grep "force_line_approval"

3. Email with attachment from shell script

uuencode $file_name $$file_name |mail -s "$EMAILSUBJECT"
$TOADDRESS -- -f $SENDERADDRESS
EMAILSUBJECT- Email Subject
TOADDRESS – To address
SENDERADDRESS – Sender Address
Note:- you need to install uuencode in your environment.

4. Get the file size in unix

FILESIZEAFTER=$(du -h $DATA_FILE | awk '{ print $1 }')
echo "... file $FINAL_OUTFILE size $FILESIZEAFTER"

5. Trim the spaces of the parameter value

USERPWD=`echo $1 | cut -f3 -d' ' | cut -f2 -d'"'`

6. Find the file file size

fsize=`wc -c $FINAL_OUTFILE`
fsize=`echo $fsize | cut -f1 -d' '`
if test $fsize -ge 1
then
echo "...Data File size > 0 "
else
echo "...Data File size = 0; "
exit
fi
else
echo "...Data File not found "
exit
fi

7. FTP Download

Mirror the directory
lftp431 -u $FTPUSER,$FTPPASS $FTPSERVER -e "mirror
$ONLY_NEWER_OPTION $REVERSE_OPTION
$REMOVE_SOURCE_FILES_OPTION $NO_PERMS_OPTION
$NO_RECURSION_OPTION --verbose=3 $SOURCEDIR
$TARGETDIR; exit"
Note:- you need to install lftp in your environment. Mirror command will download everything to your local directory (as good as mget).

8. FTP Outgoing

1. SFTP FTP- needs a port number
lftp-u $FTPUSER,$FTPPASS -e "lcd $SOURCEDIR;put
$DELETE_OPTION$EDI_OUT_DATA_FILE; exit"
$FTPSERVER:22$TARGETDIR
2. Generic FTP
lftp-u $FTPUSER,$FTPPASS -e "lcd $SOURCEDIR;put
$DELETE_OPTION$EDI_OUT_DATA_FILE; exit"
$FTPSERVER$TARGETDIR

9. Compiling the oracle form under Unix

Exporting the AU_TOP Resource path export
FORMS60_PATH=:FORMS60_PATH:$AU_TOP/resource:$AU_TOP/forms/US frmcmp_batch module=$CUS_TOP/forms/US/testfrm.fmb userid=apps/apps

R11i Command

f60gen Module=$CUS_TOP/forms/US/ testfrm.fmb Userid=apps/apps compile_all=special Output_File=$CUS_TOP/forms/US/ testfrm.fmx

10. Setting the softlink for Unix executables

ln -s $FND_TOP/bin/fndcpesr $CUS_TOP/bin/TESTSCRIPT
Where
TESTSCRIPT- is just the name of the Unix shell script without an extension.

FND LOAD for copying the AOL objects across instances Download the concurrent program


FNDLOAD apps/apps O Y DOWNLOAD $FND_TOP/patch/115/import/afcpprog.lct /tmp/TESTPROG.ldt

PROGRAM APPLICATION_SHORT_NAME="CUSTOP"
CONCURRENT_PROGRAM_NAME=" TESTPROG "
FNDLOAD apps/apps O Y UPLOAD $FND_TOP/patch/115/import/afcpprog.lct /tmp/ TESTPROG.ldt

11. SQL LOADER Example

sqlldr control=test.ctl userid=$LOGIN data=$ FILEDIR/$ DATA_FILE log=TEST.log
retcode=`echo $?`; export retcode
case "$retcode" in
0) echo "SQL*Loader execution successful" ;;
1) echo "SQL*Loader execution exited with EX_FAIL, see logfile
TEST.log" ;;
2) echo "SQL*Loader execution exited with EX_WARN, see
logfile TEST.log"
echo "All the records in $DATA_FILE will not be processed by
this program"
RET_FLAG=1 ; export RET_FLAG
;;
3) echo "SQL*Loader execution encountered a fatal error for $
DATA_FILE" ;;
*) echo "unknown return code for while processing";;
esac

12. Append files into single file

# -----------------------------------------------------------------------------+
# append files into one new file
+
#-----------------------------------------------------------------------------+
FIRST_FILE="Y"
for FILE in `ls -1 $IN_FILE_PATH/$FILE_PATTERN`
do
# --------------------------------------------------------------------------+
# get File Name by removing the path
+
# --------------------------------------------------------------------------+
FILE_NAME=`echo $FILE| awk -F\/ '{print $NF}' `
# echo "Data File name... $FILE_NAME"
# --------------------------------------------------------------------------+
# get Backup File Name
+
# --------------------------------------------------------------------------+
BK_FILE_NAME=`date +$FILE_NAME.%Y%m%d%H%M%S`
echo "...Inbound Data File name: $FILE_NAME - ...Archive
Data File name: $BK_FILE_NAME"
# --------------------------------------------------------------------------+
# append files to one new file
+
# --------------------------------------------------------------------------+
if [ "$FIRST_FILE" = "Y" ]
then
cat $FILE > $NEW_FILE
FIRST_FILE="N"
else
cat $FILE >> $NEW_FILE
fi
# --------------------------------------------------------------------------+
# move File to backup directory
+
# --------------------------------------------------------------------------+
mv $FILE $BK_DIR/$BK_FILE_NAME
done

No comments:

Post a Comment