Wednesday, 10 April 2013

Basic commands for unix shell scripting

List of Useful Commands for UNIX SHELL Scripting

We all are well aware of Unix Commands but still would like to walk you through some of the commands that we generally come across in our day to day task. Focus would be on the associates who are not very familiar with UNIX. Though simple but still would like to put some light with few commands that comes handy while scripting in UNIX which in turn is invoked by a concurrent program as a host file

Note: Unix is case sensitive.

Here I am categorizing the commands into two sections one set which can be used directly on a UNIX prompt and the one which needs to be executed a Shell Script using a Concurrent program registration.

Section 1

1.  Command to Change Directory

Syntax: cd <Path> Or cd $<Custom/Standard Top Name>_TOP (In CAPS)

Path: Here we need to mention the path or directory structure where we want to navigate.

Ex: cd /tmp

Or cd $AR_TOP (Navigates to the AR Module directory) 2. To know the Present Working Directory

Syntax: pwd

Here it would display the path/directory structure where one is currently located.

3.       To create a Directory Syntax: mkdir

4.       To list the files in a directory Syntax: ls –ltr

This is will list the files or folders that exist in the current directory where are executing the command

5.       To find a file in a directory where you are currently working Syntax: find <File Name with Directory Path>

6.       Syntax to move a file from one directory to another directory

Syntax:

mv <Source_Directory_Path><file_name> <Destination_Directory_Path><file_name>
7.  Remove a file from a where you are currently working Syntax: rm –f <file anme>

8.       Syntax for IF Statement

Syntax:

if [ <expression> ]

then

<Statement><commands>

else

<Statement><commands>

fi

9.  To fetch the value of a variable we use “$” and to display any value we use “echo”

Example: file_count=1

echo $file_count

This will return 1

10. Finding and removing file/files which is past 7 days old

Syntax:

find <Directory_path> -name "<File_Name>" -mtime +7 -exec rm -f {} \;

11. To ZIP a set of files into a .zip file

Syntax:

zip <zip_file_name>.zip <files_to zip>.txt

12. For Loop Syntax

Syntax

for <Variable> in <Command><expression> do

<Statement> done


Example:

for variable in `grep -l "^TEST*.* " <Directory_path>/*.*|cut -f 13 -d"/"` do

<Statement> Done

Above statement would search for the test TEST in the directory path and the file name that is mentioned after the 13th position of “/” (Changes based on the directory path description)


13. Searching a pattern in a file

Syntax:

grep -c '^L' <directory_path>/<File_name>

Here it searches for the test “L” in the file mentioned

14. Command to sort a file

Syntax:

sort -u <Directory_Path><File_name> > <Directory_Path><Sort_file_Name>

Section 2

Now let us know some of the advance commands that we use while scripting using UNIX Script

Let us assume that we have a requirement of creating a HOST script and registering it in the Application as a concurrent program and by invoking the Concurrent program that in turns invokes our HOST script to perform the defined task.

Commands used while scripting using UNIX and which is invoked by a concurrent program once it is registered in APPS as a HOST script would be discussed.

Suppose we have created a Concurrent Program and for instance our concurrent program are having parameters.

1.       Command used to fetch all the concurrent parameters including system defined parameters into a variable in SHELL SCRIPT

Syntax:

l_all=`echo $@`

Here l_all variable will have all the parameters of the concurrent program

Based on our requirement if we need the Request_ID of the CP and the User_Name who has submitted the Concurrent Program (CP)

Syntax:

l_request_id=`echo $l_all | tr -s " " " "|cut -f2 -d" "|cut -f2 -d"="` l_user_name=`echo $l_all | tr -s " " " "| cut -f5 -d" "| cut -f2 -d"="|sed -e 's/"/ /g'`

Here l_request_id and l_user_name would hold the value of the Concurrent program request id and the username.

Apart from the System Parameters if we have also defined our own custom parameters

Syntax:

l_param=`echo $l_all| cut -f9-300 -d' ' | sed 's/" "/"|"/g'`

Fetch all the Custom defined parameters of the CP into a variable l_param

Now for instance we have defined say 4 parameters Responsibility name, Out Directory Path, Archive Directory Path and Log directory path. To fetch these parameters into a variable

Syntax

l_resp_name=`echo $l_param | cut -f1 -d"|"|tr -d \"` l_out_dir=`echo $l_param | cut -f2 -d"|"|tr -d \"` l_archive_dir=`echo $l_param | cut -f3 -d"|"|tr -d \"` l_log-dir=`echo $l_param | cut -f4 -d"|"|tr -d \"`

2.       Now to fetch the ERP_LOGIN details which would be used if we need to connect to SQLPLUS in our SHELL SCRIPT

Syntax:

erp_login=`echo $1 | cut -d " " -f3 | cut -d "=" -f2` export erp_login

l_erp_login=`echo $erp_login | sed "s/\"//g"` -- This statement is used to truncate “ (double quotes)

3.  Now if we need to connect to SQLPLUS below is the syntax

Syntax:

sqlplus -s $<ERP_LOGIN><<ENDOFSQL <SQL STATEMENT>

exit; ENDOFSQL

Example:

sqlplus -s $l_erp_login <<ENDOFSQL set serveroutput on

SET head off SET feed off SET echo off SET pause off SET termout off

SET verify off

SELECT sysdate FROM DUAL; exit;

ENDOFSQL

Here Exit – To exist from the SQL session

L_erp_login – This variable holds the erp_login details

The above block would return the system date

4.  To send mail we use a utility “MAILX”

Syntax:

mailx -s “<Subject for a Mail>” <mail_id>

5.  Sending a mail with the Body

Syntax:

echo “<Contents_Of_body>” | mailx -s “<Subject>” <mail_ID>

6.  To connect to the remote server

Syntax:

sftp <Remote_User_Name>@<Remote_Host_Name> << EOF <Statements to execute>

bye EOF

List of commands that can be executed in a SFTP Block


Retrieve the remote-path and store it on the local machine. If the
get [flags] remote-path [local-
local path name is not specified, it is given the same name it has
path]
on the remote machine.



Upload local-path and store it on the remote machine. If the

remote path name is not specified, it is given the same name it
put [flags] local-path [local-path]
has on the local machine.


rename oldpath newpath
Rename remote file from oldpath to newpath.


ln oldpath newpath
Create a symbolic link from oldpath to newpath.


rm path
Delete remote file specified by path.


lmkdir path
Create local directory specified by path.


Bye
Quit sftp.


exit
Quit sftp.


quit
Quit sftp.


cd path
Change remote directory to path.


lcd path
Change local directory to path.



Display remote directory listing of either path or current
ls [path]
directory if path is not specified.














Pwd
Display remote working directory.
rmdir path
Remove remote directory specified by path.
chgrp grp path
Change group of file path to grpgrp must be a numeric GID.
chmod mode path
Change permissions of file path to mode.
chown own path
Change owner of file path to ownown must be a numeric UID.
symlink oldpath newpath
Create a symbolic link from oldpath to newpath.
mkdir path
Create remote directory specified by path.

Display local directory listing of either path or current directory
lls [ls-options [path]]
if path is not specified.
lpwd
Print local working directory.
lumask umask
Set local umask to umask.
command
Execute command in local shell.
!
Escape to local shell.
?
Synonym for help.
Help
Display help text
7.  Connection to the remote server can also be done using the command as below:

Syntax:

ssh -l <Remote_User_Name> <Remote_Host_Name> cd <Remote_Directory_path> \; ls '<file_name>*' |wc -l

This command would return the count of files available in the remote server

Now to remove files from the remote server

Syntax:

ssh -l <Remote_User_Name> <Remote_Host_Name> cd <Remote_Directory_path> \; rm '<File_name>*' |wc -l`

8.       While performing SFTP if the directory path doesn’t exist the error can be captured using the below command

Syntax:

sftp <Remote_User_Name>@<Remote_Host_Name> << EOF | grep -q 'No such file or directory'

9.  To check the status of the Connection if successful or not

Syntax:

status=$?

If the Status is 0 then the Connection was successful else the connection is not successful.
10.To generate a Mail with Body and subject of the mail along with the attachment which is a zip file

Syntax:

touch <mail_body>.txt

echo "<Mail_Body>" >> <mail_body>.txt

<Zipping the files(.csv files) to send as a Attachment (.zip file)> zip <file_name>.zip <files_to_zip>.csv

uuencode file_name>.zip file_name>.zip > datafile.csv cat <mail_body>.txt datafile.csv > combined.file

mailx -s "<Mail_subject>” <mail_id> < combined.file



11.   To execute SQL Loader utility to load data from a flat file to a table using a control file

Syntax:

sqlldr userid=<Erp_Login_details> control=<Control file name including directory path> data=<Data file name including directory path> log=<Path and name of the Log file>

12. To execute a registered concurrent program

Syntax:
1.   CONCSUB <APPS username>/<APPS password> \

2.   <responsibility application short name> \

3.   <responsibility name> \

4.   <Oracle Applications username> \

5.   [WAIT=N|Y|<n seconds>] \

6.   CONCURRENT \

7.   <program application short name> \

8.   <program name> \[PROGRAM_NAME=”<description>”]\

9.   [REPEAT_TIME=<resubmission time>] \

10.  [REPEAT_INTERVAL= <number>] \

11.  [REPEAT_INTERVAL_UNIT=< resubmission unit>] \

12.  [REPEAT_INTERVAL_TYPE=< resubmission type>] \

13.  [REPEAT_END=<resubmission end date and time>] \

14.  [START=<date>] \

15.  [IMPLICIT=< type of concurrent request> \

16.  [<parameter 1> ... <parameter n>]

1 comment:

  1. Your information is amazing. If you want to know that how to zip a directory in Unix then
    visit this link for more information;

    how to zip a directory in Unix

    ReplyDelete

CREATING A SUPPLIER IN R12 (Functional and SUPPLIERS IN TCA)

Who is a Supplier? Let us now understand who is a Supplier with the help of a simple example: We consider us as a business, who sell...