-

Bourne Shell: Chapter 1

-

Please contact or Mt Xia for assistance with all your shell programming needs.


  • 1. BOURNESHELL OVERVIEW Go to the bottom, first,previous,next, last section, table of contents.

    1. BOURNESHELL OVERVIEW

    The BourneShell is both a command-line interpreter and a high- level programming language. When it is acting as a command-line interpreter, it processes commands as you enter them at the command prompt. When you use it as a programming language, it processes commands that are stored in files known as BourneShell scripts. This course will show you how to create and execute BourneShell scripts. We will explore BourneShell programming including such features as variables, control structures, processes, and executable files. The BourneShell is one of three shells available on most UNIX systems. Bourne is the accepted standard for System V UNIX. The other shells are being used more and more. The other shells are the CShell and the KornShell. The CShell is BSD (Berkeley Software Distribution) UNIX. BSD was developed at the University of California at Berkeley, California. Most of the features found in the BourneShell are also found in the other shells; there are differences, however. The CShell and KornShell are not standard on UNIX System V but are generally available. BourneShell scripts allow you to group command lines together and execute them by entering a single command at the command line. This allows complex functions to be completed by any user, and repetitive functions can be completed easily. Input and output can also be redirected from a BourneShell script.

    1.1 What is the BourneShell?

    BourneShell is a high level programming language and a command line interpreter. The command to invoke the BourneShell is:
      Command Format:  sh [-acefhiknrstuvx] [args]                 
                                                                   
      (See Appendix A for a complete list of options etc)
    
    A Shell script is an executable plain file that contains UNIX and shell commands. To execute the shell script type the name of the script at the prompt. A simple shell script called shell_ex is shown in the following example. The output from the execution of the shell is also shown. Sample Session: $cat shell_ex echo "This is a very simple shell procedure " echo "created with the basic echo command " echo "and three other very basic commands " echo ps echo who echo ls $sh shell_ex This is a very simple shell procedure created with the very basic echo command and three other very basic commands PID TTY TIME COMMAND 10443 rt02120 0:01 sh 10427 rt02120 0:04 ksh sgavlick rt021e0 Sep 7 13:26 teacher rt021b0 Sep 7 14:39 memo class_notes $

    1.2 Making a Bourne Shell Script Executable

    A BourneShell script is an ordinary file that contains commands which can be executed in sequence by entering one command at the BourneShell prompt. In order for a script to be executed, it must first be executable. This is done with the chmod command. Sample Session: $cat shell_ex echo "This is a very simple shell procedure " echo "created with the basic echo command " echo "and three other very basic commands " echo ps echo who echo ls $ If the ls -l shell_ex command were entered, we would see the protections assigned to this file. Sample Session: $ls -l shell_ex -rw-r--r-- 1 teacher class 66 Sep 7 10:24 shell_ex $ The character in column one is the type of file. - = ordinary (plain) disk file d = directory b = block special file c = character special file p = fifo file ("named pipe") special file l = symbolic link Notice that the script file in the previous sample session has the following file protections: User - Read and Write Group - Read Other - Read No execute permissions have been granted for user, group, or other. If we try to execute this script by typing its name, the following would result. Sample Session: $shell_ex shell_ex: execute permission denied $ This error message would indicate that execute permission was denied. The BourneShell script could not be executed. To change the permissions for the BourneShell script, use the chmod command. Sample Session: $chmod 755 shell_ex $ls -l shell_ex -rwxr-xr-x 1 teacher class 66 Sep 7 10:26 shell_ex $ Now that the permissions have been changed to allow user, group, and others to execute the file, it will execute properly. Sample Session: $shell_ex This is a very simple shell procedure created with the basic echo command and three other very basic commands PID TTY TIME COMMAND 10443 rt02120 0:01 sh 10427 rt02120 0:04 ksh sgavlick rt021e0 Sep 7 13:26 . teacher rt021b0 Sep 7 14:39 . . The protections will work as you expect. Execute permission for the user will allow you (the owner) to run the BourneShell script. Group permissions allow anyone in your group to execute the script, and other permission allows anyone on the system to execute the script.

    1.3 Tracing Mechanisms

    It is possible to have a trace made of the BourneShell script as it executes. This is invaluable for debugging purposes. All that is required is to give an option to the BourneShell. This is done by including an option on the call to "sh". The command to do this is:
         Command Format: sh [-acefhiknrstuvx] [args]               
                                                                   
         See Appendix A for a complete list of options etc        
    
    The option to turn on tracing is -x. For an example, let's trace the execution of the simple script shell_ex. Sample session: $cat shell_ex echo "This is a very simple shell procedure " echo "created with the basic echo command " echo "and three other very basic commands " echo ps echo who echo ls $ Execute the BourneShell script using the -x option on the call to the shell. The following sample session shows how to do this and it shows the results of the trace. Sample session: $sh -x shell_ex + echo This is a very simple shell procedure This is a very simple shell procedure + echo created with the basic echo command created with the basic echo command + echo and three other very basic commands and three other very basic commands + echo + ps PID TTY TIME COMMAND 10443 rt01120 0:01 sh 10427 rt02120 0:04 ksh + echo + who sgavlick rt021e0 Sep 7 13:26 teacher rt02120 Sep 7 14:39 + echo + ls memo class_notes $ The commands as read from the BourneShell script are indicated by the plus sign (+). The next line or lines are the results of the execution of the command. Using this tracing option allows you to se the execution of each command in the script and see the results of that execution.

    Workshop 1

    This workshop will reinforce your understanding of the ideas presented in Chapter 1. Each student is to complete the entire workshop. DESK EXERCISES 1. The BourneShell can act as a command line or a high level . 2. The BourneShell is one of three shells generally available. What are the other two? 3. One advantage of using a shell script is . 4. The command to call the BourneShell is: a. bourne b. ksh c. b d. sh 5. Why would you use tracing? 6. What UNIX command do you enter to make a BourneShell script executable? That's all

    Go to the top, first,previous,next, last section, table of contents.