UNIX Shell Scripting
UNIX Shell Scripting
Day - 1
Topics
About UNIX
UNIX OS
Files, Process and Directory Shell Anatomy
List Commands
Directory Commands Search and Control Commands
About UNIX
What is Unix
UNIX is a powerful, portable, multi-tasking and multi-user computer operating system
Unix Advantage
Unix is more flexible and can be installed on many different types of machines, including mainframe computers, supercomputers and micro-computers. Unix is more stable and does not go down as often as Windows does, therefore requires less administration and maintenance. Unix has greater built-in security and permissions
UNIX OS
Unix OS: UNIX operating system is made up of three parts; the kernel, the shell and the programs.
Kernel The kernel of UNIX is the hub of the operating system: it allocates time and memory to programs and handles the files store and communications in response to system calls.
Shell The shell acts as an interface between the user and the kernel The shell is a command line interpreter(CLI) Key Flavor's : Bourne Shell, Bash Shell and Korn Shell
Program The commands are themselves programs Multiple commands can be combined as a script
Directory Structure The file-system is arranged in a hierarchical structure, like an inverted tree. The top of the hierarchy is traditionally called root (written as a slash / )
Basic Commands:
List File/Directory : To find out what is in your home directory, type % ls
% ls -all
Commands (cont.)
Directory Commands:
Change Directory % cd directory
Commands (cont.)
File Manipulation Commands:
Display File Contents % cat filename
Variable Substitution:
Creating a Variable % set var
Commands (cont.)
I/O Commands:
Unix treats every device as a file In effect, terminal and keyboard can be treated as files Keyboard is an input file Terminal monitor is an output file Unix treats every device as a file
Every program has three default files stdin, stdout, and stderr These files are respectively numbered as 0, 1, and 2 command 1>&2 > [Link]
Search Commands:
Search a string in a file % grep apple [Link]
Commands (cont.)
Control Constructs:
The flow of control within Shell scripts is done through constructs
If..Then..Elif..Else..
do list done
fi
Example Example: #!/bin/sh #!/bin/sh if [ "$1" = "1" ] count=$1 then while [ $count -gt 0 ] echo "Parameter value is 1" do elif [ "$1" = "2" ] count=$(expr $count -1) then
done
echo "Parameter value is 2" elif [ "$1" = "3" ] then echo "Parameter value is 3" else echo "Parameter value is $1" fi
Commands (cont.)
Control Constructs:
For
Case
Example: !#/bin/sh case $1 in 1) echo 'First Choice';; 2) echo 'Second Choice';; *) echo 'Other Choice';; esac
Commands (cont.)
Functions: Anatomy:
name() { commands }
Example: printlog() { echo "$1" }
Control statements
if [ "$1" = " Hello World " ] then echo $1"
fi
Day - 2
Topics
vi Editor
FTP
SSH (including SCP and SFTP) Unix Mailer - mailx
Vi Editor
vi Editor:
vi is a screen-oriented text editor originally created for the Unix operating system
FTP (cont)
put put <filename> mput <filename*.*>
Transfer Mode
ascii For text file like data files and Shell Scripts
bin For executable like .rdf, .fmx etc.
SSH
SSH:
SSH, which is an acronym for Secure SHell, was designed and created to
vs
SSH (cont.)
Secure Copy:
SCP is a means of securely transferring computer files between a local host and a remote host or between two remote hosts. It is based on the Secure Shell (SSH) protocol. Copying file to host: scp SourceFile user@host:directory/TargetFile Copying file from host: scp user@host:directory/SourceFile TargetFile
SFTP:
SFTP is a network protocol that provides file access, file transfer, and file management functionalities.
Key features : 1. Delete remote file 2. Execute a shell in the remote 3. Interactive
Unix Mailer
mailx: mailx: An utility to send and receive email. SMTP/mail server setting has to be preconfigured prior to mailx usage Options: 1. Mail body 2. Attach files 3. Add cc and bcc
Why fndcpesr? - Otherwise we need to read the concurrent program parameters using cut commands
Step 4 : Create a executable with execution type as Host Step 5 : Create a concurrent program and attach the executable and create parameters if required
Example: APPS_LOGIN=$1 USERID=$2 USERNAME=$3 REQUESTID=$4 USER_PARAM1=$5 USER_PARAM2=$6 USER_PARAM3=$7 USER_PARAM4=$8 USER_PARAM5=$9 SHIFT USER_PARAM6=$9 SHIFT USER_PARAM7=$9 For Completed - Warning: ???????????? For Completed - Error: exit2
Execute DB Procedure
Execute DB Procedure:
sqlplus -s $DB_CONNECT << EOF > ${TEMPFILE0} SET ECHO OFF
p_file_archive_flag => lv_file_archive_flag ); dbms_output.put_line (' File Archive Flag :'|| lv_file_archive_flag); EXCEPTION
Day - 3
Topics
CONCSUB Utility
Password less login through SSH File Encryption/Decryption
Examine the data and build the Control file accordingly and load the data into the table
Example:
cd $CUSTOM_TOP/data
for file in sample*.dat do sqlldr userid=$login control=$CUSTOM_TOP/bin/sv_test.ctl
data=$CUSTOM_TOP/data/$file
mv $CUSTOM_TOP/data/$file $CUSTOM_TOP/archive/$file done
CONCSUB
CONCSUB
Concurrent program can be submitted from Shell script through concsub utility shipped by Oracle
Example:
cd $FND_PATH v_concreq=`./CONCSUB $ORA_USER_PASS $APPLN_NAME $RESP_NAME1 $USER_NAME WAIT=Y CONCURRENT SQLAP "APXMCCDF3" $CARD_PROGRAM_ID ${1}`
SSH Password-less Login: SSH provides password less login mechanism which is useful in process automation and batch processing
Sensitive data requires file encryption/decryption to maintain security when it is transferred through a computer network
PGP/GPG
PGP/GPG
Pretty Good Privacy (PGP) / GNU Privacy Guard is a data encryption and decryption computer program that provides cryptographic privacy and authentication for data communication.
Encrypt Command: /opt/pgp/bin/pgp --encrypt ${FILE_NAME}.txt --output ${FILE_NAME}.pgp --overwrite remove -passphrase ${PGPPASSPHRASE} -r ${PGP_RECIPIENT}
Decrypt Command: /opt/pgp/bin/pgp --decrypt --input ${FILE_NAME} --output ${DECRYPT_FILE_NAME}.dat --overwrite remove --passphrase ${PGPPASSPHRASE}
Work around:
sqlplus -s $p_usr_pwd <<-EOF set feedback off declare l_stat Boolean; l_session_id Number;
begin
fnd_global.initialize(session_id => l_session_id, user_id => fnd_global.user_id, resp_id => fnd_global.resp_id , resp_appl_id => fnd_global.resp_appl_id, security_group_id => null , site_id => null, login_id => null, conc_login_id => null , prog_appl_id => null, conc_program_id => null, conc_request_id => $p_req_id, conc_priority_request => null); l_stat := fnd_concurrent.set_completion_status('WARNING','Completed in Warning. Review log for details.'); commit; end; / exit; EOF
References
Reference URLs
[Link] [Link] [Link] [Link] [Link]
Q&A
Q&A
Thank You
[Link]