UNIX Bourne Shell Programming Developed by: User Liaison Section, D-7131 Denver Office [Name and number removed at author's request] Revision Date: February 7, 1991 I. INTRODUCTION.............................................. v A. Audience............................................. v B. Course Objectives.................................... v C. Course Handout Conventions........................... vi 1. BOURNESHELL OVERVIEW...................................... 1 1.1 What is the BourneShell?............................ 2 1.2 Making a Bourne Shell Script Executable............. 3 1.3 Tracing Mechanisms.................................. 6 Workshop 1............................................... 9 2. USER, SHELL, AND READ-ONLY SHELL VARIABLES................ 11 2.1 User Variables...................................... 11 2.2 Shell Variables..................................... 14 2.2.1 HOME......................................... 14 2.2.2 IFS.......................................... 15 2.2.3 MAIL......................................... 15 2.2.4 MAILPATH..................................... 15 2.2.5 MAILCHECK.................................... 16 2.2.6 PATH......................................... 16 2.2.7 PS1.......................................... 17 2.2.8 PS2.......................................... 17 2.3 Read-Only User Variables............................ 18 2.4 Read-Only Shell Variables........................... 19 2.4.1 Name of the Calling Program.................. 19 2.4.2 Arguments.................................... 19 2.4.3 Shift........................................ 21 2.4.4 Set.......................................... 22 2.4.5 expr......................................... 23 Workshop 2............................................... 27 3. POSITIONAL PARAMETERS..................................... 33 3.1 Reading Input Into a Shell Variable................. 34 3.2 Command Substitution................................ 36 3.3 Comments in BourneShell Scripts..................... 38 3.4 BourneShell Environment - Exporting Variables....... 39 Workshop 3............................................... 41 4. CONTROL CONSTRUCTS:....................................... 45 4.1 Types of Tests Used with Control Constructs:........ 46 4.2 Test on Numeric Values.............................. 47 4.3 Test on Character Strings........................... 47 4.4 Test on File Types.................................. 49 4.5 if then............................................. 50 4.6 if then else........................................ 52 4.7 if then elif........................................ 54 4.8 for................................................. 55 4.9 while............................................... 57 4.10 until.............................................. 58 4.11 case............................................... 60 Workshop 4............................................... 63 5. COMPILING PROGRAMS IN UNIX................................. 67 5.1 "C": Sample Program with a Main and Two Functions in One ................................. 67 5.2 "C": Compiling a Program............................ 69 5.3 "C": Renaming the Executable Module................. 71 5.4 "C": Giving a Name to the Output File............... 72 5.5 "C": Producing an Assembly Listing.................. 73 5.6 "C": Main and Two Functions in Three Separate Source Files.......................................... 74 5.7 "C": Compiling but Not Producing an Executable Module............................................. 75 5.8 FORTRAN: Sample Program a Main and Two Subroutine... 76 5.9 FORTRAN: Compiling a Program........................ 77 5.10 FORTRAN: Renaming the Executable Module............ 79 5.11 FORTRAN: Giving a Name to the Output File.......... 80 5.12 FORTRAN: Producing an Assembly Listing............. 81 5.13 FORTRAN: Main and Two Subroutines in Three Separate Source Files.............................. 82 5.14 FORTRAN: Compiling But Not Producing an Executable Module.................................... 83 5.15 FORTRAN: Compiling Object Files to Produce an Executable Module............................. 84 5.16 COBOL: Sample Program with a Main and Two Subroutines................................. 85 5.17 COBOL: Compiling a Program......................... 86 5.18 COBOL: Running a Program........................... 87 Workshop 5............................................... 89 6. UNIX TOOLS................................................ 95 6.1 Processes........................................... 95 6.2 Executing a Command................................. 95 6.3 Process Identification.............................. 95 6.4 grep: A Pattern Matching Filter..................... 98 6.4.1 More on Regular Expressions.................. 99 6.4.2 Closure......................................103 6.4.3 Some Nice grep Options ......................104 6.4.4 Summary of Regular Expression Characters.....105 6.5 sed: Edit a File to Standard Output.................106 6.6 awk: A Pattern Matching Programming Language........110 6.7 sort: Sort a File...................................114 6.8 What Other Useful UNIX Tools are Available..........117 6.9 Archiver and Library Maintainer.....................118 6.9.1 ar: Creating an Archive File with Object Modules.....................................119 6.9.2 ar: Verifying the Contents of the Archive File.......................................119 6.9.3 ar: Removing Duplicate Object Files..........120 6.9.4 ar: Compiling Main and Archive Files.........120 Workshop 6...............................................121 7. VAX DCL TO UNIX SHELL SCRIPT CONVERSION...................125 7.1 Processes...........................................127 7.2 Pipes...............................................128 7.3 Input, Output, and Error Redirection................129 7.4 Command Structure and File Naming Conventions.......131 7.5 File Management Commands............................133 7.6 Metacharacters......................................135 7.7 Wildcards: Are They Really Wild?....................136 7.8 Summary.............................................137 Workshop 7...............................................139 8. ADVANCED FEATURES OF FTP..................................143 8.1 Initializing FTP on UMAX............................144 8.2 Multiple File Transfers.............................145 8.3 Auto Login Feature..................................146 8.4 Macros..............................................148 8.5 Filename Translation................................149 8.6 Aborting Transfers..................................150 8.7 More Remote Computer Commands.......................151 Workshop 8...............................................153 9. OPTIONAL CHAPTER - KORNSHELL PROGRAMMING..................155 9.1 KornShell Variables.................................155 9.2 User Defined Variables..............................157 9.3 Values of Variables Between Child and Parent Processes...........................................158 9.4 ksh: Aliases........................................159 9.5 ksh: Command Line Editing...........................161 9.6 ksh: Interactive Command Line Editing...............162 9.7 ksh: Functions......................................164 9.8 ksh: The Select Construct...........................166 9.9 ksh: Tracing and Conditional Execution..............168 Workshop 9...............................................169 APPENDIX A - sh...............................................173 APPENDIX B - test.............................................189 APPENDIX C - expr.............................................193 APPENDIX D - ftp..............................................195 APPENDIX E - cc...............................................209 APPENDIX F - f77..............................................219 APPENDIX G - lint.............................................231 APPENDIX H - cb...............................................235 APPENDIX I - ar...............................................237 APPENDIX J - time.............................................243 APPENDIX K - ksh..............................................245 INDEX.........................................................279 I. INTRODUCTION A. Audience This course is for individuals who have completed "UNIX for Beginning Users" (or equivalent experience) and want to write UNIX BourneShell script files. A script file contains a sequence of UNIX commands which can be executed by entering one command. It is assumed that the student already has a good understanding of the UNIX operating system, be able to use a UNIX editor, and be familiar with a computer terminal or typewriter keyboard. B. Course Objectives Upon successful completion of this course the student will be able to: 1. Write moderately complex BourneShell scripts. 2. Make a BourneShell script executable. 3. Demonstrate how to use the following BourneShell commands: shift, exit, expr, test, if then, if then else, if then elif, for, while, until, and case. 4. Use the following BourneShell constructs: tracing mechanisms (for debugging), user variables, BourneShell variables, read-only variables, positional parameters, reading input to a BourneShell script, command substitution, comments, and exporting variables. In addition, test on numeric values, test on file type, and test on character strings are covered. 6. Create a ".profile" script to customize the user environment. 7. Use advanced features of File Transfer Protocol (FTP) 8. Compile source code into object and executable modules. 9. Optional: KornShell programming. This is of primary interest to programmers. 10. Convert VMS DCL command files to UNIX Shell. C. Course Handout Conventions There are several conventions used in this handout for consistency and easier interpretation: 1. Samples of actual terminal sessions are single-lined boxed. 2. User entries are shown in bold print and are underlined. exit 3. All keyboard functions in the text will be bold. (Ret) Backspace Tab Ctrl-F6 Print (Shift-F7) Go to DOS (1) NOTE: (Ret) indicates the Return or Enter key located above the right Shift key. 4. Examples of user entries not showing the computer's response are in dotted-lined boxes. 5. Command formats are double-lined boxed. 6. Three dots either in vertical or horizontal alignment mean continuation or that data is missing from diagram. ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ ³ ³ Multimax, Nanobus, and UMAX are trademarks of ³ ³ Encore Computer Corporation. ³ ³ ³ ³ ³ ³ Annex is a trademark of XYLOGICS, Inc. ³ ³ ³ ³ ³ ³ UNIX and Teletype are registered trademarks of ³ ³ AT&T Bell Laboratories ³ ³ ³ ³ ³ ³ Ethernet is a trademark of Xerox Corporation ³ ³ ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ NOTES ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ 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. NOTES ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ 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 NOTES ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ 2. USER, SHELL, AND READ-ONLY SHELL VARIABLES The BourneShell has no true numeric variables. It uses string variables to represent numbers, as well as text. String variables are able to take on the value of a string of characters. There are three types of variables in the BourneShell. They are user variables, BourneShell variables, and Read-only BourneShell variables. You can declare, initialize, read, and modify user variables from a BourneShell script or from the command line. The BourneShell itself declares and initializes shell variables, but you can read and modify them. The BourneShell also initializes the read-only shell variables, and you can read but not modify them. 2.1 User Variables It is legal to assign any sequence of non-blank characters as the name of a variable. The sample session below creates a variable called person and initializes it with the string Richard. It is important to note that you must NOT precede or follow the equal sign with a space or TAB character. Sample Session: ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ $person=Richard ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ This sample session indicates that person does not represent the