Steps to Create Shell Programs


When you craft a shell program, there are some initial steps you want to perform so that you have consistency among all of your programs. The following is a list of some important concepts to employ with your shell programs:

  1. Names of shell programs and their output - You should give your shell programs and their output a meaningful name . If you call your shell programs script1, script2, and so on, these will not have any meaning for you and other potential users of the programs. If your shell program finds files of a particular type, then you can name the program filefind or some other descriptive name. Do the same with the output of shell programs. Don't call the output of a shell program output1 if there is some descriptive name you can give it such as filefind.out . Also avoid naming shell programs and their output names that already exist. You may use commands such as read and test , so you may create confusion and conflicts if you were to give your program and their output the same names. The first shell program in this chapter shows today's date. If we were to name the program date , we would actually run the system date command as opposed to our shell program date . The reason is that in most cases, the system date command would be found before our shell program date file.

  2. Repository for shell programs - If you plan to produce numerous shell programs, you may want to place them in a directory. You can have a directory in your home directory called bin or shellprogs , in which all programs are located. You can then update your path to include the directory name where your shell programs are located.

  3. Specify shell to execute program - The very first line of the program specifies the shell to be used to execute the program. The path of the shell is preceeded by #! , which is a "magic number" that indicates that the path of the shell is about to follow. The shell programs in this chapter use the ksh path.

  4. Formatting of shell programs - Do not underestimate the importance of proper formatting to enhance the readability of shell programs. You should indent areas of the program to indicate that commands are part of a group . Indenting only three or four spaces is fine for groups of commands. You may also want to set autoindent in vi so that the next line starts at the same point as the previous line (try :set ai in vi to see how this technique works). You can break up long lines by placing a \ (backslash) at the end of one line and continue the command on the next line.

  5. Comments - Include detailed comments in your shell program. You can write paragraphs of comments to describe what an upcoming section of your program will accomplish. These will help you understand your program when you look at it months or years after having originally created it as well as help others understand the program. No limit exists on the number of comments you can include. Start comment lines with a # ( pound sign).

  6. Make the shell program executable - The chmod command covered in the previous chapters introducing various shells is used to make your program executable.

Following the previous list of recommendations will make creating and using shell programs more efficient. Now we need to cover the types of shell programs.

A shell program is simply a file containing a set of commands you wish to execute sequentially. The file needs to be set with execute permissions so that you can execute it just by typing the name of the script.

There are two basic forms of shell programs exist:

  1. Simple command files - When you have a command line or a set of command lines that you use over and over, you can use one simple command to execute them all.

  2. Structured programs - The shell provides much more than the ability to "batch" together a series of commands. It has many of the features that any higher-level programming language contains:

    • Variables for storing data

    • Decision-making controls (the if and case commands)

    • Looping abilities (the for and while loops )

    • Function calls for modularity

Given these two basic forms you can build everything from simple command replacements to much larger and more complex data manipulation and system administration tools.



HP-UX 11i Systems Administration Handbook and Toolkit
HP-UX 11i Systems Administration Handbook and Toolkit (2nd Edition)
ISBN: 0131018833
EAN: 2147483647
Year: 2003
Pages: 301

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