Unix Shell Script For Oracle Apps


What is a Shell Script?

A shell is an environment in which we can run our commands, programs, and shell scripts. There are different flavors of shells, just as there are different flavors of operating systems. Each flavor of shell has its own set of recognized commands and functions.

The basic concept of a shell script is a list of commands, which are listed in the order of execution. A good shell script will have comments, preceded by a pound sign, #, describing the steps. Unix is case sensitive. Because Unix is case sensitive our shell scripts are also case sensitive.

Shell scripts and functions are both interpreted. This means they are not compiled. Both shell scripts and functions are ASCII text that is read by the Korn shell command interpreter. When we execute a shell script, or function, a command interpreter goes through the ASCII text line by line, loop by loop, test by test and executes each statement, as each line is reached from the top to the bottom.


Unix Special Characters

All of the following characters have a special meaning or function. If they are used in a way that their special meaning is not needed then they must be escaped. To escape, or remove its special function, the character must be immediately preceded with a backslash, \, or enclosed within ‘ ‘ forward tic marks (single quotes).
                                                       
\ ( ; # $ ? & * ( ) [ ] ` ‘ “ +
Different Types of Shells to Declare

#!/usr/bin/sh         OR          #!/bin/sh Declares                a Bourne shell
#!/usr/bin/ksh       OR          #!/bin/ksh Declares             a Korn shell
#!/usr/bin/csh       OR          #!/bin/csh Declares              a C shell
#!/usr/bin/bash     OR          #!/bin/bash Declares           a Bourne-Again shell

Control Structures

Similar to C language, The following control structures will be used extensively.

if ... then Statement                                       if ... then ... else Statement                           
if [ test_command ]                                                            if [ test_command ]
then                                                                                        then
commands                                                                            commands
fi                                                                                             else
                                                                                                commands
fi





if ... then ... elif ... (else) Statement
if [ test_command ]
then
commands
elif [ test_command ]
then
commands
elif [ test_command ]
then
commands
.
.
.
else (Optional)
commands
fi
for ... in Statement
for loop_variable in argument_list
do
commands
done

while Statement
while test_command_is_true
do
commands
done
Until Statement
until test_command_is_true
do
commands
done
Case Statement
case $variable in
match_1)
commands_to_execute_for_1
;;
match_2)
commands_to_execute_for_2
;;
match_3)
commands_to_execute_for_3
;;
.
.
.
*) (Optional - any other value)
commands_to_execute_for_no_match
;;
esac




Using break, continue, exit, and return
It is sometimes necessary to break out of a for or while loop, continue in the next block of code, exit completely out of the script, or return a function’s result back to the script that called the function. break is used to terminate the execution of the entire loop, after completing the execution of all of the lines of code up to the break statement. It then steps down to the code following the end of the loop.
continue is used to transfer control to the next set of code, but it continues execution of the loop.
exit will do just what one would expect: It exits the entire script. An integer may be added to an exit command (for example, exit 0), which will be sent as the return code.
return is used in a function to send data back, or return a result, to the calling script.

Shell Script Commands

The basis for the shell script is the automation of a series of commands. We can execute most any command in a shell script that we can execute from the command line.
COMMAND         DESCRIPTION
passwd                   Change user password
pwd                        Print current directory
cd                            Change directory
ls                             List of files in a directory
wildcards              * matches any number of characters, ? matches a single character
file                          Print the type of file
cat                          Display the contents of a file
pr                            Display the contents of a file
pg or page            Display the contents of a file one page at a time
more                      Display the contents of a file one page at a time
clear                      Clear the screen
cp or copy            Copy a file
chown                    Change the owner of a file
chgrp                     Change the group of a file
chmod                   Change file modes, permissions
rm                          Remove a file from the system
mv                          Rename a file
mkdir                    Create a directory
rmdir                     Remove a directory
grep                       Pattern matching
egrep                     grep command for extended regular expressions
find                        Used to locate files and directories
>>                           Append to the end of a file
>                              Redirect, create, or overwrite a file
|                               Pipe, used to string commands together
||                              Logical OR—command1 || command2—execute command2 if command1 fails
&                            Execute in background
&&                         Logical AND—command1 && command2—execute command2 if command1 succeeds
date                        Display the system date and time
echo                       Write strings to standard output
sleep                       Execution halts for the specified number of seconds
wc                           Count the number of words, lines, and characters in a file
head                       View the top of a file
tail                          View the end of a file
diff                         Compare two files
sdiff                       Compare two files side by side (requires 132-character display)
spell                       Spell checker
lp, lpr, enq, qprt Print a file
lpstat                      Status of system print queues
enable                    Enable, or start, a print queue
disable                   Disable, or stop, a print queue
cal                          Display a calendar
who                        Display information about users on the system
w                             Extended who command
whoami                 Display $LOGNAME or $USER environment parameter
who am I               Display login name, terminal, login date/time, and where logged in
f, finger                 Information about logged-in users including the users .plan and .project
talk                         Two users have a split screen conversation
write                      Display a message on a user’s screen
wall                        Display a message on all logged-in users’ screens
rwall                      Display a message to all users on a remote host
rsh or remsh        Execute a command, or log in, on a remote host
df                            Filesystems statistics
ps                            Information on currently running processes
netstat                    Show network status
vmstat                   Show virtual memory status
iostat                      Show input/output status
uname                   Name of the current operating system, as well as machine information
sar                          System activity report
basename             Base filename of a string parameter
man                        Display the on-line reference manual
su                            Switch to another user, also known as super-user
cut                          Write out selected characters
awk                        Programming language to parse characters
sed                          Programming language for character substitution
vi                            Start the vi editor
emacs                    Start the emacs editor

Variables

Avariable is a character string to which we assign a value. The value assigned could be a number, text, filename, device, or any other type of data. A variable is nothing more than a pointer to the actual data.

Symbol Commands
( )                            Run the enclosed command in a sub-shell
(( ))                          Evaluate and assign value to variable and do math in a shell
$(( ))                       Evaluate the enclosed expression
[ ]                            Same as the test command
[[ ]]                          Used for string comparison
$( )                          Command substitution
`command`           Command substitution

Math in a Shell Script

We can do arithmetic in a shell script easily. The Korn shell let command and the ((expr)) command expressions are the most commonly used methods to evaluate an integer expression. Later we will also cover the bc function to do floating-point arithmetic.

Cron Tables

A cron table is a system file that is read every minute by the system and will execute any entry that is scheduled to execute in that minute. By default, any user can create a cron table with the crontab -e command, but the Systems Administrator can control which users are allowed to create and edit cron tables with the cron.allow and cron.deny files. When a user creates his or her own cron table the commands, programs, or scripts will execute in that user’s environment. It is the same thing as running the user’s $HOME/.profile before executing the command.
The crontab -e command starts the default text editor, vi or emacs, on the user’s cron table.

How to use ftp in a shell script:

Sometimes we want to FTP a file from one machine to another. Usually, we can do the transfer interactively, but every so often, a shell script do the file transfer.
The problem we always encountered in scripting ftp transfers involved getting a password to the ftp server. Typical ftp client programs under Unix, Linux, Solaris and NetBSD all read the ftp password from /dev/tty.

Example Working Script

#!/bin/sh
HOST='ftp.users.qwest.net'
USER='yourid'
PASSWD='yourpw'
FILE='file.txt'

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $FILE
quit
END_SCRIPT
exit 0

The Tricks

Getting the password to the ftp server without having the ftp client program read the password from /dev/tty requires two tricks:
Using the -n option on the ftp client program to prevent the ftp client from trying to log in immediately. That way, the ftp client does not ask for a user ID and password. No use of /dev/tty.
Use the ftp client program command quote to send user ID and password to the ftp server.

Further Refinements

The above sh script will spew lots of ftp client output to standard output. Even if everything works perfectly, the user running the above script will see lots of incomprehensible text scrolling by quite rapidly. One refinement would send output to different places:

ftp -n $HOST > /tmp/ftp.worked 2> /tmp/ftp.failed <<END_SCRIPT

One could further refine error handling by acting on the ftp client program's exit status:
ftp -n $HOST > /tmp/ftp.worked 2> /tmp/ftp.failed <<END_SCRIPT
blah blah
END_SCRIPT

EXITSTATUS=$?

if [ $EXITSTATUS != "0" ]
then
    # handle the error...
fi
Except that the above doesn't always work - most FTP clients always exit with a status of 0. This leads to ugly "false negatives": the file transfer fails, but the script doesn't detect the problem.
One way to verify that a file transfer took place - transfer it back:
#!/bin/sh

ftp -n << END_SCRIPT
open $1
user $2 $3
put $4
get $4 retrieval.$$
bye
END_SCRIPT

if [ -f retrieval.$$ ]
then
                echo "FTP of $4 to $1 worked"
                rm -f retrieval.$$
else
                echo "FTP of $4 did not work"
fi
Regular FTPs there and back of large files can consume a lot of time.

Control of ftp by a shell script

One obvious improvement would have the ftp client program controlled by the shell script. I don't think that would comprise an impossible task, but we also don't think that it would have much value. Scripting ftp transfer using expect might cause you less pain.

How to Submit Concurrent Program Using CONCSUB:
Syntax:

CONCSUB <ORACLE ID>
        <ResponsIbility Application Short Name>
        <ResponsIbility Name>
        <User Name>
        CONCURRENT
        <Concurrent Program Application Short Name>
        <Concurrent Program Name>
        [START=<Requested Start Date>]
        [REPEAT_DAYS=<Repeat Interval>]
        [REPEAT_END=<Request Resubmission End Date>]
        <Concurrent Program Arguments ...>

Example to shutdown the concurrent manager

CONCSUB APPLSYS/FND SYSADMIN  'System Administrator' SYSADMIN [WAIT={Y|N}]
CONCURRENT FND [SHUTDOWN|ABORT]

ORACLE ID: Enter the username and password of the ORACLE ID you want your concurrent program to  
                       run in.  Enter the username and password without any spaces and separated by a slash ("/").

Responsibility Application Short Name: Enter the short name of the application for your responsibility.    
                       This name, along with your responsibility name, will be used to select a responsibility for    
                       your concurrent request to run in.

Responsibility Name: Enter the name of your responsibility. This name, along with your responsibility
                       application short name, will be used to select a responsibility for your concurrent request to
                       run in.

User Name: Enter the name of your Application ObjectLibrary user.  This name will be used to update the
                      Who information for any data your concurrent program changes and to create report output
                      file name for this request.

Concurrent Program Application Short Name: Enter the short name of the application for your concurrent
                     program. This name, along with the concurrent program name, uniquely identifies the
                     concurrent program for your concurrent request.

Concurrent Program Name: Enter the name of your concurrent program. This name, along with the  
               concurrent program application short name, uniquely identifies the concurrent program for your
               concurrent request.

Start Date: Specify the token START and enter the date you want the concurrent request to run, in DD-
               MON-YY HH24:MI:SS format. Omit this argument if you want to run your concurrent request
               immediately.^M^M<Repeat Interval> Specify the token REPEAT_DAYS and enter the time
               interval, in days, for request resubmission. Use fractional number for intervals less than a day.
               Omit this argument if you want to run your concurrent request only once.

Request Resubmission End Date: Specify the token REPEAT_END and enter the date you want the request
              resubmission to stop, in DD-MON-YY HH24:MI:SS format.  This argument is used in conjunction
              with "Repeat Interval". Omit this argument if you want your request to repeat forever at an interval
             specified by REPEAT_DAYS.

Concurrent Program Arguments: Enter the arguments for your concurrent program.

Sample HOST File to Submit Two concurrent Programs Using CONCSUB:
########################################################################
# Set the variables for running concurrent program.
########################################################################
CONCPRG="SQL_CONC_TEST"
CONCAPP="SEAR"
RESPAPP="SEAR"
RESP="SMK-AR-AFFILIATE"
USERNAM=`echo $1 | cut -d ' ' -f 5  | cut -d '"' -f 2`

echo $CONCPRG
echo $CONCAPP
echo $RESPAPP
echo $RESP
echo $USERNAM
########################################################################
CONCMSG=`noarg CONCSUB $FCP_LOGIN \
                                                $RESPAPP \
                                                $RESP \
                                                $USERNAM \
                                                WAIT=10 \
                                                CONCURRENT \
                                                $CONCAPP \
                                                $CONCPRG`
########################################################################
# Set the variables for running concurrent program.
########################################################################
CONCPRG="PLSQL_CONC_TEST"
########################################################################
CONCMSG=`noarg CONCSUB $FCP_LOGIN \
                                                $RESPAPP \
                                                $RESP \
                                                $USERNAM \
                                                WAIT=10 \
                                                CONCURRENT \
                                                $CONCAPP \
                                                $CONCPRG`
reqid=""
reqid=`echo $CONCMSG | cut -d " " -f 3`
CONCMSG=`echo $CONCMSG | cut -d'"' -f3`
y=""
y=`echo $reqid | grep "^[0-9]"`
if [ $? -ne 0 ]
then
    echo "Failed to schedule SMARCONSS thru Control-M option ...\n"
                exit 1
                else
                    exit 0
                    fi


In the above script we have taken two concurrent programs named SQL_CONC_TEST and PL/SQL_CONC_TEST both consists simple PL/SQL code. These are assigned to “SMK-AR-AFFILIATE” responsibility and its short name is “SEAR”. Here we have taken two CONCSUB commands to submit the concurrent programs.

Mailing using UNIX Shell Script:

     mailx - interactive message processing system

SYNOPSIS
     mailx [-BdeHiInNURvV~] [ -f  [file | +folder] ] [-T file] [-u user]

     mailx [-BdFintUv~] [-b bcc] [-c cc] [-h number] [-r address] [-s subject] recipient...

     /usr/ucb/mail ...

     /usr/ucb/Mail ...

DESCRIPTION

The mail utilities listed above provide a comfortable, flexible environment for sending and receiving mail messages electronically.

When reading mail, the mail utilities provide commands to facilitate saving, deleting, and responding to messages. When sending mail, the mail utilities allow editing, reviewing and other modification of the message as it is entered. Incoming mail is stored in a standard file for each user, called the mailbox for that user. When the mail utilities     are called to read messages, the mailbox is the default place to find them. As messages are read, they are marked to be moved to a secondary file for  storage,  unless  specific     action  is  taken,  so  that  the  messages need not be seen again. This secondary file is called the mbox and is normally located in the user's HOME directory (see MBOX in ENVIRONMENT VARIABLES for a description of this file). Messages can be saved in other secondary files named by the user. Messages remain in a secondary file until forcibly removed.

The user can access a secondary file by using the -f option. Messages in the secondary file can then be read or otherwise processed using the same Commands as in the primary mailbox. This gives rise within these pages to the notion of a current mailbox.

OPTIONS
On the command line options start with a dash (-). Any other arguments are taken to be destinations (recipients). If no recipients are specified, mailx attempts to read messages     from the mailbox.

     -B    Do not buffer standard input or standard output.

     -b bcc  Set the blind carbon copy list to bcc. Bcc should be enclosed in quotes if it  
          contains more than one name.
     -c cc  Set the carbon copy list to cc. cc should be enclosed in quotes if it contains more 
               than one name.
     -d    Turn on debugging output. (Neither particularly interesting nor recommended.)
     -e    Test for the presence of mail. mailx prints nothing and exits with a successful
            return code if there is mail to read.

     -F    Record the message in a file named after the first recipient. Overrides the record
             variable, if set (see Internal Variables).

     -f [file]   Read messages from file instead of mailbox. If no file is specified, the mbox
                    is used.

     -f [ +folder]   Use the file folder in the folder directory  (same as the folder command).
                           The  name of this directory is listed in the folder variable.

     -H    Print header summary only.

     -h number   The number of network "hops" made so far. This is provided for network
              software to avoid infinite delivery loops. This option and its argument are passed
              to the delivery program.

     -I    Include the newsgroup and article-id header lines when printing mail messages.
            This option requires the –f  option to be specified.

     -i    Ignore interrupts. See also ignore in Internal Variables.

     -N  Do not print initial header summary.

     -n   Do not initialize from the system default mailx.rc or Mail.rc file.

See USAGE.: Set the Subject header field  to subject. Subject should be enclosed in quotes if it contains embedded white space.

     -T file  Message-id and article-id header lines are recorded in file  after the message is
                 read. This option also sets the -I option.

     -t    Scan the input for To:,  Cc:,  and  Bcc:  fields.  Any recipients on the command
            line will be ignored.

     -U   Convert UUCP-style addresses  to  internet  standards. Overrides the conv
            environment variable.
     -u user
           Read user's mailbox. This is only effective if  user's mailbox is not read protected.

     -V    Print the mailx version number and exit.

     -v    Pass the -v flag to send mail (1M).

     -~    Interpret tilde escapes in the input even if not reading from a tty.
    
INT_CODE=`echo $1 | cut -f1 -d' '`
INT_REQID=`echo $1 | cut -f2 -d' ' | cut -f2 -d'='`
INT_REQID=`expr $INT_REQID \* 1`
LOGIN=`echo $1 | cut -f3 -d' ' | cut -f2 -d '"'`
USERNAME=`echo $1 | cut -f5 -d' ' | cut -f2 -d'"'`
FUSER=`echo $USERNAME | cut -c1-8`
PER_NAME=`echo $1 | cut -f9 -d' ' | cut -f2 -d '"'`
echo $INT_CODE
echo $INT_REQID
echo $LOGIN
echo $USERNAME
echo $FUSER
echo $PER_NAME

var=`echo $INT_CODE $INT_REQID $LOGIN $USERNAME $FUSER  $PER_NAME`
mailx -m -s $var oracle.apps@gmail.com

Comments

Popular posts from this blog

Oracle APPS Useful Queries

How to Apply Timers In Forms