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