unix shell scripting - in an oracle environment

Download Unix Shell Scripting - In an Oracle Environment

Post on 22-Oct-2014

91 views

Category:

Documents

3 download

Embed Size (px)

TRANSCRIPT

UNIX SHELL SCRIPTING IN AN ORACLE ENVIRONMENT

By William A. Ducat Ambassador, Inc.5299 DTC Boulevard Suite 290 Englewood, CO 80112 (888) 775-3778 x227 (Voice) (630) 839-5264 (Fax)

www.rmoug.org

RMOUG Training Days 2001

UNIX Shell Scripting for Oracle

Ducat

Table of Contents

Introduction................................................................................................................................................. 3 Conventions ............................................................................................................................................ 3 Building Blocks .......................................................................................................................................... 4 Concepts.................................................................................................................................................. 4 STDOUT............................................................................................................................................. 4 STDERR ............................................................................................................................................. 4 STDIN ................................................................................................................................................. 5 Environment Variables ....................................................................................................................... 6 Scripts.................................................................................................................................................. 6 Parameter Passing ............................................................................................................................... 8 Flow Control ....................................................................................................................................... 9 Identifying your process.................................................................................................................... 10 Practical Examples .................................................................................................................................... 11 Password Management ......................................................................................................................... 11 Embedded SQL..................................................................................................................................... 12 Embedded Subroutines (aka Functions) ............................................................................................... 13 Passing Parameters................................................................................................................................ 15 Global Functions ................................................................................................................................... 18 Conclusion ................................................................................................................................................ 22

Sample CodeFigure 1 - Redirecting STDERR to a file.................................................................................................... 4 Figure 2 - Redirecting STDERR to STDOUT............................................................................................ 4 Figure 3 - Redirecting STDIN in a script.................................................................................................... 5 Figure 4 - Basic script ................................................................................................................................. 7 Figure 5 Sample setup_passwords.ksh script......................................................................................... 11 Figure 6 - PS results on HP or Sun ........................................................................................................... 12 Figure 7 - PS results on Compaq or Linux ............................................................................................... 12 Figure 8 - Embedding SQL in a KSH script ............................................................................................. 13 Figure 9 - Implementing a user-defined function in a KSH script ........................................................... 14

www.rmoug.org

RMOUG Training Days 2001

UNIX Shell Scripting for Oracle

Ducat

IntroductionThe primary purpose of this paper is to provide a starting point for your own scripting projects. To accomplish this, the paper is divided into two major sections. The Building Blocks section will describe some of the required basic concepts, and the Practical Examples section takes these basic constructs and steps through the construction of a rather useful script. The script starts out as a very basic script, and by applying multiple new features, becomes a very flexible and useful tool. Developing scripts for the Oracle environment involves performing some rather complex tasks, and the KORN shell naturally lends itself to the task. Because of this, and its wide availability, we will use the KORN shell for all examples. In the UNIX environment, a users interactive environment or scripts always run in some sort of shell. A shell can be thought of as an interface to the underlying UNIX command set. Most tasks can be completed in any available shell, but each shell has various attributes, which tend to make it desirable for various applications. For instance, the Bourne shell (/bin/sh) is a very basic shell, which is common to most UNIX installations. Many install programs use the BOURNE shell since one script can be used, unchanged, on multiple platforms. The C shell (/bin/csh) has various features to assist in the interactive environment, and many users make the C shell their default 1 shell for this reason. The KORN shell (/bin/ksh) lends its self to heavy scripting, and includes the ability to define functions that are local to a script, as well as functions that are available to the users interactive session. Another relative newcomer in the shell arena is the BASH shell (/bin/bash)(a.k.a Bourne Again Shell). BASH attempts to combine the best of the C and BOURNE shells into one. BASH is not widely used at this time, but has the potential to become very popular.

ConventionsIn this paper when a space is critical, the b symb ol will be used to denote each space. When something is enclosed in a box, as is this paragraph, it contains commands to enter, the text of a UNIX script, or the results of a script or command.

1

The default shell for a user is specified on the users entry in the /etc/passwd file.

www.rmoug.org

RMOUG Training Days 2001

UNIX Shell Scripting for Oracle

Ducat

Building BlocksUNIX can be thought of as a very thin operating system. Instead of providing a series of very powerful commands with numerous options, such as the VMS operating system, it provides a large number of very simple commands which can be strung together to obtain the desired result. A simple script is nothing more than a series of commands or strings of commands executed in a sequential manner. A complex script uses flow control constructs and conditional operators to control the execution of commands much like a 3GL such as C, BASIC or Fortran.

ConceptsNumerous texts cover the syntax and purpose of all of the UNIX commands, and we will not attempt to cover all of them here. We will however cover some of the more useful ones from a scripting standpoint, as well as some basic issues related to all commands. There are three things common to most commands. First, they need a source of input. Second, they need somewhere to send their output. Third, they need somewhere to send the results of any errors encountered. In the UNIX world, we have standard input (STDIN), standard output (STDOUT), and standard error (STDERR). The default values for each command differ, but as a rule, STDOUT and STDERR are sent to the screen, and STDIN will be requested interactively from the user, or from a file. STDOUT When performing a command such as ls, the results of the listing are displayed to the screen. This can be changed by redirecting STDOUT with the > redirector. For instance, if the ls >listing.out command is issued, the file listing.out will be created if it does not exist, or it will be replaced if it does exist. When complete, the file will contain the results of the ls command. When the > is used, the results will not be shown on the screen. The >> redirector works like > except it will append the to a file if it already exists. STDERR If an ls command returns no files found, the resulting error message is shown on the screen. If the same command is performed except the results are redirected to a file, the resulting file will be empty and the error message will still be displayed on the screen. The reason is that the results are being sent to STDOUT, and the error is sent to STDERR. Take the following command as an example: ls myfile* >results.txt 2>errors.txt

Figure 1 - Redirecting STDERR to a fileSTDERR is referenced via the 2> redirector. In this case, results are sent to the results.txt file, and errors are sent to the errors.txt file. In a given command, STDOUT can be referenced as &1. Consider the following command: ls myfile* >results.txt 2>&1

Figure 2 - Redirecting STDERR to STDOUTIn this case, the results.txt file will contain both the results and the errors (if any), since STDERR is redirected to STDOUT.

www.rmoug.org

RMOUG Training Days 2001

UNIX Shell Scripting for Oracle

Ducat

STDIN Standard input typically comes from one of three

Recommended

View more >