Chapter 10. Introduction to Shell Programming

   

Chapter Syllabus

10.1 Anatomy of a Shell Program

10.2 Using Variables

10.3 Command Line Arguments

10.4 Interactive Shell Programs

10.5 Exit Codes

10.6 The test Command

10.7 Branching

Shell programs or shell scripts are files containing HP-UX commands, comments, and control structures. These are like batch processes where commands are interpreted and executed by the shell line-by-line . Any command that can be executed at the shell prompt can be included in a shell program. Comments begin with a pound character ( # ) and are not executed by the shell. The POSIX shell allows use of control structures that can be utilized for branching and looping within the shell program. These control structures use test conditions for conditional branching and looping.

The shell provides very powerful features for writing shell programs. Many of the shell programs are used at startup and shutdown time and will be discussed in Chapter 13. These programs are shipped with HP-UX and provide control over many critical tasks related to system administration.

Shell programs are also used by system administrators to automate routine jobs. Files containing the shell commands are executed at specified times using the HP-UX cron facility. Mostly shell programs help in controlling the size of log files, cleaning temporary files, and reporting errors to the system administrator through email.

In this chapter, you will start with simple programs and analyze the parts of a typical shell program. Then you will learn how variables can be used to pass information to a shell program. Command line parameters can be passed to a program and utilized within it. The command line parameters may be useful for passing data or control information that is used during execution. Many times you may need to interact with your shell programs during the execution. You may also need to enter some values at run time. Interactive programs are very helpful for such applications, and you will learn how to read user input during their execution. When a program finishes execution, it returns a result code to the shell that is used to determine if the program terminated successfully. You will learn the use of exit codes, which are used to report any errors that occurred during the execution.

Variables are used to store values temporarily within the programs. In Section 10.2, you will learn how to utilize these variables inside shell programs. Before you can use some control structures, you need to test a condition. The result of this test allows you to make a decision. In the last part of the chapter, you will see how to use the test command and make decisions depending on its result.

This chapter includes the basic features of shell programming. The terms shell program and shell script are used interchangeably in this and the next chapter. In the next chapter, you will find some more-complicated shell programs containing loop structures.


   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

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