• No results found

Anatomy of a Shell Program

In document Hp-Ux Unix Rehman (Page 116-119)

Table 6-2. Meta Characters Used in Regular Expressions Charact

Chapter 7. File Permissions 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.

10.1 Anatomy of a Shell Program

Let us go directly to our first program and analyze it. I have used the file name script-00 for this program. Contents of this file are shown below using the cat command.

$ cat script-00

#!/usr/bin/sh

# This is to show what a script looks like.

echo "Our first script"

echo "---"

echo # This inserts an empty line in output.

echo "We are currently in the following directory"

pwd echo

echo "This directory contains the following files"

ls

$

Before looking into the program and explaining what each line does, let us see what happens if we execute it. We execute it from the current directory with the command line:

$ ./script-00 Our first script ---

We are currently in the following directory /home/boota

This directory contains the following files

PHCO_18132.depot myfile phco_18132.txt PHCO_18132.text phco_18131.txt script-00

$

The program prints some messages on your terminal screen and then shows the current directory and lists files in this directory.

Let us have a closer look at it. Basically, any shell program has three parts.

the full path of the subshell that will be used to execute the program some comment lines

commands and control structures I will explain these one-by-one.

Which Subshell Will Execute It?

The current shell executes all programs unless otherwise specified. In case you need to execute a program in a specific shell (Bourne, C, or POSIX), you can specify it in your program. In that case, a subshell is created as soon as a program starts execution. The first program line shows which HP-UX shell will be used to execute commands found in the program. This line always starts with the "#!" character combination and shows the full path of the executable program that will be used as shell. All HP-UX extrinsic commands have the same syntax no matter which shell is used to execute them. The difference is in the execution of intrinsic commands. For example, the method of setting a shell variable in the C shell is different from the one used in the POSIX shell. So you need to execute your program in the proper shell. Depending on the information provided, your current shell will spawn the appropriate subshell to execute the program. As an example, you can't use the setenv command in a program that is expected to be run in a POSIX or Bourne shell because this command is specific to the C shell only.

In the example used here, the subshell that will be used to execute the program is /usr/bin/sh, which is the POSIX shell. You can use other shells, such as C, by changing this to /usr/bin/csh.

It is a good habit to provide the shell name in the program so that if somebody else is using it in a different shell, the correct subshell is created and the program runs without any error.

Comments in a Shell Program

The second line in our example program contains a comment. A comment is that part of a program that is not executed. It is used for providing reference information about the program.

All comments start with a pound sign (#) except for the special combination used in the first line for specifying the subshell. A comment can be placed anywhere in the file. If a line starts with the "#" sign, all of the line is treated as a comment. If the "#" sign is placed somewhere else in a line, anything after that sign is considered a comment. In the example program script-00, comments are used in the second and fifth lines. The second line starts with the "#" sign, so all of the line is a comment, and nothing in this line is executed. The fifth line contains a command echo and after that a comment string. The command is executed but the comment is ignored.

Commands and Control Structures

This is the most important part of the program, where you put actual commands that are executed. The commands may be simple ones that the shell executes one-by-one, in the order they are placed in the program file. In the example program, we have used the commands pwd, echo, and ls. All of these commands are executed in order and their result is displayed on your terminal screen as you have already seen. The echo command without any argument just prints a blank line.

The control structures are used for branching and looping. The decision of branching or looping is made depending on the result of a test performed on some variables or constants.

We will discuss branching at the end of this chapter and looping in the next chapter.

Steps for Creating a Shell Program

A shell program is created in two basic steps. In the first step, a file is created that contains commands and control structures. This file is saved on the disk. Usually this file is not executable. In the second step, you need to modify file permissions to make it executable. If you are not sharing the program with others, you can use the chmod u+x command to make it executable. If you are sharing your program with other users, you need to set the appropriate permissions for this purpose.

You can also execute a shell program without the execute bit set if you use the program name as an argument to sh as shown below.

$ sh script-00

After setting appropriate execute permissions, you can execute a program. Care must be taken while naming shell programs such that the names do not match with any existing HP-UX commands.

If the current directory is not included in the PATH variable, you will not be able to execute the program by simply typing its name on the command line. For that purpose you need to specify the full path of the file. You can give the full path on the command line in either the absolute or relative form. The better way is the relative form, where you use "./" (dot slash) to refer to the current directory.

Note

Many times new script writers wonder why the script is not being executed, even though they have placed the correct commands and have the execution bit set. The reason is the directory in which they are placing the program is not included in the PATH variable, and they are not specifying the path to the file explicitly. Sometimes it may happen that your current directory is included in the PATH variable at the end. When you execute your program without specifying the full path to the file, you get unexpected results. This is the case when you use a file name for a program that already exists on your system. What happens is the shell starts searching

the file name from the first directory specified in your PATH variable. It gets the other file before it reaches the current directory and executes it. So it is always recommended to use "./"

when you are testing your program for the first time to make sure that the shell is indeed executing the correct file.

Debugging Shell Programs

Debugging shell programs is a tricky business. There are many simple to complex procedures that can be applied for this purpose. Here is the simplest and basic method. You replace the first line of the program #!/usr/bin/sh with #!/usr/bin/sh -x. After that, when you execute the program, it displays each line on your terminal screen before executing it. The actual line present in the program is shown with a plus (+) sign in the start of the line. After that, its output is displayed. This method can be used to identify which program line is causing a problem.

Below is the output of our example program script00 after this modification. Note that comments are not displayed.

$ ./script-00

+ echo Our first script Our first script

+ echo --- ---

+ echo

+ echo We are currently in the following directory We are currently in the following directory

+ pwd

/home/operator + echo

+ echo This directory contains the following files This directory contains the following files

+ ls

PHCO_18132.depot myfile phco_18132.txt PHCO_18132.text phco_18131.txt script-00

$

Study Break

Writing a Shell Program

If you are new to shell programming, this is the time to write your first shell program. Use the vi editor to create a new file named myscript. Use only one command (who) to list users currently logged into the system. Save the file and set its execution bit using the command chmod u+x myscript. Try to execute it and see if it is doing what you intend it to do.

In document Hp-Ux Unix Rehman (Page 116-119)