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 .

  1. the full path of the subshell that will be used to execute the program

  2. some comment lines

  3. 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.


   
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