Module 108 Programming Constructs amp

Previous Table of Contents Next


Module 108
Programming Constructs ” Shell Programming

DESCRIPTION

Shell programming constructs are used to build shell scripts. In this module we discuss the use of various shell programming constructs and the basics of creating executable shell scripts.

Before you name your shell script, verify that is does not already exist on the system. You can use the whence , type , or which command, depending on your login shell, to see if your current PATH can locate a command with the same name. If no command is found, then you can use the name you selected.

If you work in a heterogeneous environment, you will probably want to locate the shell with the most features common to all systems and program to that standard. A heterogeneous environment consists of several types of hardware and various versions of UNIX. A homogeneous environment consists of at least all the same versions of the operating systems. The hardware may or may not be different. So you can have heterogeneous or homogeneous hardware mixed with heterogeneous or homogeneous software.

If you write shell scripts that are used on all of the systems, you will want to consider the problem of porting and maintaining the scripts. Although you may not be able to use the latest and greatest shell features of one system, by using a shell common to all systems, you will make life easier for yourself and the next person who has to perform your job.


NOTE:    
We only cover the constructs of the ksh in this module. The sh is a subset of the Korn shell. The csh shell is not commonly used for shell programming, thus it is not discussed. I am sure csh lovers will disagree ! The constructs are listed next to the ksh constructs for those who believe they must use the csh to program with.


NOTE:    
If the first character of a file is a # (number sign) the script is executed as a csh script. If it is not a # the script is executed using the /bin/sh shell. You can also specify a type of interpretor by using #! interpretor on the first line of the script file. Where interpretor might be /bin/sh, /bin/ksh, /bin/csh, /bin/awk, etc.

COMMAND FORMAT

Following is the general format of a shell script command.

 shell_script_name [ options ] [ arguments ] 

Since you create a shell script you select the name. The options and arguments depend on your implementation of the programming solution.

Options

Since you create a shell script you decide what options are needed. When processing options within a shell script you should always use the getopts command when it is available. The getopts command is supported on System V Release 3 and later revisions.


BSD (Berkeley)
Most BSD systems DO NOT support the getopts command. Therefore, you have to process the options using a while or for loop.

Arguments

Arguments should also be processed using the getopts command. Again you write the script, so you decide what arguments are valid.

A LOCAL BIN DIRECTORY

Before you start creating local shell scripts you might consider making a directory to store your shell scripts. For example,

 cd      mkdir bin      chmod 755 bin 

creates a directory named bin in your HOME directory. You will need to update your .profile to have the new directory in your PATH. For example, edit your $HOME/.profile, search for the last occurence of PATH (:g/PATH/p), and add the following line.

 PATH=$PATH:~/bin            # For Korn shell      PATH=$PATH:$HOME/bin        # For Bourne shell 

This adds your personal bin directory to your path. Now cd to bin to create new shell script commands.

CREATING A SHELL SCRIPT

The following steps outline the creation of a simple shell script.


NOTE:    
Don t forget that the shell offers the ability to alias commands. Since aliases are much faster than shell scripts you might consider using aliases instead of one- or two-line shell scripts.

To create a shell script you simply enter the editor of your choice. The filename should be the name you want to use to invoke your shell script. For example, let s say you want a shell script to list all users in your group . You might want to call the command lg for list groups. To create the script invoke vi with lg as an argument.

 vi lg 

Once in the editor, you write the necessary commands to perform the desired task. In this case the commands would look something like the following code.

 OIFS=$IFS      IFS=":"      set 


Illustrated UNIX System V
Illustrated Unix System V/Bsd
ISBN: 1556221878
EAN: 2147483647
Year: N/A
Pages: 144
Authors: Robert Felps

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net