TUCoPS :: Unix :: General :: bourne1.txt

Unix Bourne Shell Programming



















                   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 
string Richard.  The string person is echoed as person.  The
BourneShell will only do the substitution of the value of the
variable when the name of the variable is preceded with a dollar
sign ($).  

Sample Sesssion:
                
ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³  $echo person                                                 ³
³  person                                                       ³
³  $echo $person                                                ³
³  Richard                                                      ³
³  $                                                            ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
If you want to have imbedded spaces in a variable, it is necessary
to quote the string.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³  $person='Richard and Kathleen'                               ³
³  $echo $person                                                ³
³  Richard and Kathleen                                         ³
³  $                                                            ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

The echo utility copies its arguments to the standard output.  The
command echo $person displays the value of the variable person. 
It will not display $person because the BourneShell doesn't pass
$person as an argument.  The leading dollar sign ($) causes the
BourneShell to substitute the value of the variable and then passes
that value to the utility.  The echo utility then displays the
value of the variable, not its name, never knowing that you called
it with a variable.  The BourneShell passed the same command line
as if you had typed in echo Richard and Kathleen.  The BourneShell
can be prevented from doing this substitution by entering one of
the following:

Sample Sessions:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³  $echo $person                                                ³
³  Richard and Kathleen                                         ³
³  $                                                            ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
                                                                 
In this session the contents of the variable person are displayed.
The BourneShell made the substitution because the variable name
person is preceded by a dollar sign ($).

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³  $echo \$person                                               ³
³  $person                                                      ³
³  $                                                            ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

In the above example the variable person is preceded by a dollar
sign ($) but the dollar sign has a backslash (\) ahead of it. The
backslash has the effect of cancelling the special meaning of the
character following the backslash. In this case, the special
meaning of the dollar sign is ignored and the substitution is not
done.




ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³  $echo '$person'                                              ³
³  $person                                                      ³
³  $                                                            ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ


The single quote marks (') causes the characters between the marks
to be taken as literal. The shell makes no attempt to interpret the
meanings of these characters. The shell passes these characters on
with no substitution. 

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³  $echo "$person"                                              ³
³  Richard and Kathleen                                         ³
³  $                                                            ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

The double quote marks do not prevent the shell from making
substitution; and the value of the variable will be displayed by
the utility.
2.2  Shell Variables


The BourneShell declares and initializes variables that determine
such things as your home directory, what directories the shell will
look in when you give commands, how often to look for mail, your
prompt, and many other things.  We will look at several of these
BourneShell variables and their functions.  You can assign new
values to these variables from the command line or from the
execution of the .profile file in your home directory.

2.2.1  HOME

The first BourneShell variable that we will look at is the HOME
variable.  By default, the home directory is the current working
directory after you login.  The system administrator determines
your home directory when you establish an account and places that
information in the /etc/passwd file.  When you login, the
BourneShell gets that pathname and assigns it to the HOME variable.

When you enter a cd command with no argument, the utility takes
the name of the directory from the HOME variable and makes it the
current working directory.  If you change the HOME variable to
another directory pathname, the utility will make the new directory
the current working directory.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $echo $HOME                                                   ³
³ /user0/rharding                                               ³
³ $cd                                                           ³
³ $pwd                                                          ³
³ /user0/rharding                                               ³
³ $HOME=/user0/rharding/eng                                     ³
³ $cd                                                           ³
³ $pwd                                                          ³
³ /user0/rharding/eng                                           ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

This example shows how the value of the HOME variable affects the
cd utility.  The cd command will use the value of the HOME variable
as the pathname for the current working directory.
2.2.2  IFS

This is the internal-field separator BourneShell variable.  You
can always use a space or tab to separate characters on the command
line.  When you assign the IFS variable to another character, you
can also use this character as the field separator.

Example:
.................................................................
. $num_args a:b:c:d                                             .
.................................................................

This example shows only one argument, namely a:b:c:d.
.................................................................
.  $IFS=:                                                       .
.  $num_args a:b:c:d                                            .
.................................................................

This example now shows four different arguments; each being
separated by the new IFS, (:).

2.2.3  MAIL

The MAIL variable contains the name of the file that the mail (and
mailx) utilities use to store your mail.  Usually, the absolute
pathname of this file is /usr/mail/name, where name is your login
name.

Example:
.................................................................
. $MAIL=/usr/mail/rharding                                      .
.................................................................

2.2.4  MAILPATH

This variable contains a list of filenames separated by colons. If
set, the BourneShell will inform you when any of these files are
modified (i.e. when new mail arrives).  Normally, this variable is
not set.
2.2.5  MAILCHECK

This variable specifies how often, in seconds, the BourneShell will
check for new mail.  The default is 600 seconds.  If set to 0, it
will check for new mail each time before it gives you a prompt.

2.2.6  PATH

This BourneShell variable will describe the directories that will
be searched looking for the program that you want to execute.  The
BourneShell looks in several directories for a file that has the
same name as the command that you entered.  The PATH variable
controls this search path. Normally, the first directory searched
is the current working directory.  If the program is not found,
the search continues in the /bin and then the /usr/bin directory. 
Generally, these directories contain executable programs.  If the
program is not found in one of these directories, the BourneShell
reports that the program can't be found (or executed).

The PATH variable lists the pathnames in the order in which the
search will proceed.  The pathnames are separated by a colon (:).
If nothing (null string) precedes the colon, that indicates to
start the search at the current working directory.

Example:
.................................................................
. $PATH=:/user0/rharding/bin:/bin:/usr/bin                      .
. $                                                             .
.................................................................

This PATH variable indicates to start the search for the program
at the current working directory, then look in the directory
/user0/rharding/bin, then /bin, and finally /usr/bin.

If each user has a unique path specified, each user can execute a
different program by giving the same command.  The search for the
program stops when it is satisfied; thus, you can use the same name
for your own programs as the standard UNIX utilities.  To do this,
simply put your program in one of the first directories that the
BourneShell searches.
2.2.7  PS1

This is the BourneShell prompt which lets you know that the shell
is waiting for you to give it a command.  The default BourneShell
prompt is a dollar sign ($).  The shell stores the prompt as a
string variable in PS1.  When you change the value of this
variable, the appearance of the prompt will change.  When you are
working on several different machines, it might be useful to have
the prompt be the name of the machine you are working on.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $pwd                                                          ³
³ /user0/rharding                                               ³
³ $PS1='domax0: '                                               ³
³ domax0:                                                       ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

Notice that prompt is now domax0:

2.2.8  PS2

This variable is called the secondary prompt.  If the command is
not completed on one line and must be continued on the next line,
the prompt for that continued line is PS2.  The default is >. This
prompt indicates that the BourneShell is expecting you to finish
the previous command line.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $echo 'demonstration of prompt string                         ³
³ >2'                                                           ³
³ demonstration of prompt string                                ³
³ 2                                                             ³
³ $PS2='Continue? '                                             ³
³ $echo 'demonstration of                                       ³
³ Continue? prompt string 2'                                    ³
³ demonstration of                                              ³
³ prompt string 2                                               ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

Notice how the secondary prompt was changed to "Continue? ".
2.3  Read-Only User Variables

The contents of the user variables and the shell variables can be
modified by the user.  It is possible to assign a new value to
them.  The new value can be assigned from the dollar ($) prompt or
from inside a BourneShell script.  Read-only variables are
different.  The value of read-only variables can not be changed.

The variable must be initialized to some value; and then, by
entering the following command, it can be made read only.


ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
º  Command format:    readonly variable_name                    º
º                                                               º
º  variable_name = name of the variable to be made read only    º
ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $person=Kathleen                                              ³
³ $readonly person                                              ³
³ $echo $person                                                 ³
³ Kathleen                                                      ³
³ $person=Richard                                               ³
³ person: is read only                                          ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

The readonly command given without any arguments will display a
list of all the read-only variables.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $person=Kathleen                                              ³
³ $readonly person                                              ³
³ $example=Richard                                              ³
³ $readonly example                                             ³
³ $readonly                                                     ³
³ readonly person                                               ³
³ readonly example                                              ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
2.4  Read-Only Shell Variables

The read-only shell variables are similar to the read-only user
variables; except the value of these variables is assigned by the
shell, and the user CANNOT modify them.


2.4.1  Name of the Calling Program

The shell will store the name of the command you used to call a
program in the variable named $0.

It has the number zero because it appears before the first argument
on the command line.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $cat name_ex                                                  ³
³ echo 'The name of the command used'                           ³
³ echo 'to execute this script was' $0                          ³
³ $name_ex                                                      ³
³ The name of the command used                                  ³
³ to execute this script was name_ex                            ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

2.4.2  Arguments

The BourneShell will store the first nine command line arguments
in the variables named $1, $2, ..., $9.  These variables appear in
this section because you cannot change them using the equal sign. 
It is possible to modify them using the set command.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $cat arg_ex                                                   ³
³ echo 'The first five command line'                            ³
³ echo 'arguments are' $1 $2 $3 $4 $5                           ³
³ $arg_ex Richard Kathleen Douglas                              ³
³ The first five command line                                   ³
³ arguments are Richard Kathleen Douglas                        ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

The script arg_ex will display the first five command-line
arguments.  The variables representing $4 and $5 have a null value.

The BourneShell variable $* represents all of the command-line
arguments as shown in the following example.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $cat display_all                                              ³
³ echo $*                                                       ³
³ $display_all Richard Kathleen Douglas                         ³
³ Richard Kathleen Douglas                                      ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

The BourneShell variable $# contains the number of arguments on
the command line.  This is a string variable that represents a
decimal number.  You can use the expr utility to perform
calculations with that number and test to perform logical tests on
it.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $cat num_args                                                 ³
³ echo 'This script was called with'                            ³
³ echo $# 'arguments'                                           ³
³ $num_args Richard Kathleen Douglas                            ³
³ This script was called with                                   ³
³ 3 arguments                                                   ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
2.4.3  Shift

The shift command promotes each of the command-line arguments. 
The second argument, represented by $2, is now the first argument,
represented by $1.  The third becomes the second and so on until
the last argument becomes the next to last.  You can access only
the first nine command-line arguments (as $1 through $9).  The
shift command gives you access to the tenth, and the first becomes
unavailable.  There is no "unshift" command that will return the
arguments that are no longer available.

Sample Session: 

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $cat demo_shift                                               ³
³ echo 'arg1='$1 '  arg2='$2 '   arg3='$3                       ³
³ shift                                                         ³
³ echo 'arg1='$1 '  arg2='$2 '   arg3='$3                       ³
³ shift                                                         ³
³ echo 'arg1='$1 '  arg2='$2 '   arg3='$3                       ³
³ shift                                                         ³
³ echo 'arg1='$1 '  arg2='$2 '   arg3='$3                       ³
³ shift                                                         ³
³ $demo_shift Richard Kathleen Douglas                          ³
³ arg1=Richard  arg2=Kathleen  arg3=Douglas                     ³
³ arg1=Kathleen arg2=Douglas   arg3=                            ³
³ arg1=Douglas  arg2=          arg3=                            ³
³ demo_shift: cannot shift                                      ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

The BourneShell will display an error message when the script
executes a shift command after it has run out of variables.
2.4.4  Set


The Set command will display a list of all the variables that are
set when it has no arguments.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $set                                                          ³
³ HOME=/user0/teacher                                           ³
³ IFS=                                                          ³
³                                                               ³
³ LOGNAME=richard                                               ³
³ MAIL=/usr/mail/richard                                        ³
³ MAILCHECK=600                                                 ³
³ PATH=:/bin:/usr/bin                                           ³
³ PS1=$                                                         ³
³ PS2=>                                                         ³
³ SHELL=/bin/sh                                                 ³
³ TERM=vt100                                                    ³
³ TZ=MST7MDT                                                    ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

When set is called with arguments, it sets the value of the
command-line arguments ($1-$n) to the arguments.  The example sets
the first three arguments.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $cat set_ex                                                   ³
³ set who really cares                                          ³
³ echo $#: $*                                                   ³
³ $set_ex                                                       ³
³ 3: who really cares                                           ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
2.4.5  expr

The expr command will perform arithmetic in the BourneShell.

ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
º   Command format:  expr expression                            º
º                                                               º
º   See Appendix C for a complete list of expressions           º
ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ

The arguments are taken as an expression. After the evaluation has
taken place, the result is written to standard output.  The terms
of the expression must be separated by blanks.  Special characters
to the shell must be escaped.  Strings containing blanks or other
special characters must be quoted.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $expr 7 + 8 + 10                                              ³
³ 25                                                            ³
³ $expr 10 - 8                                                  ³
³ 2                                                             ³
³ $expr 10 '*' 4                                                ³
³ 40                                                            ³
³ $expr 135 / 5                                                 ³
³ 27                                                            ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

expr will also work with user defined variables as in the following
example:

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $cat data                                                     ³
³ 8                                                             ³
³ 15                                                            ³
³ 25                                                            ³
³ $cat express                                                  ³
³ count=0                                                       ³
³ tot=0                                                         ³
³ for a in `cat data`                                           ³
³ do                                                            ³
³ tot=`expr $tot + $a`                                          ³
³ count=`expr $count + 1`                                       ³
³ done                                                          ³
³ avg=`expr $tot / $count`                                      ³
³ echo "The average is $avg"                                    ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
Let's execute the script "express" with tracing on so we can follow
the execution.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $sh -x express                                                ³
³ count=0                                                       ³
³ tot=0                                                         ³
³ + cat data                                                    ³
³ + expr 0 + 8                                                  ³
³ tot=8                                                         ³
³ + expr 0 + 1                                                  ³
³ count=1                                                       ³
³ + expr 8 + 15                                                 ³
³ tot=23                                                        ³
³ + expr 1 + 1                                                  ³
³ count=2                                                       ³
³ + expr 23 + 25                                                ³
³ tot=48                                                        ³
³ + expr 2 + 1                                                  ³
³ count=3                                                       ³
³ + expr 48 / 3                                                 ³
³ avg=16                                                        ³
³ + echo The average is 16                                      ³
³ The average is 16                                             ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
                               NOTES
ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ
                               NOTES
ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ
Workshop 2

This workshop will reinforce your understanding of the ideas
presented in Chapter 2. Login to the Multimax using the username
and password given to you by the instructor.  Each student is to
complete the entire workshop.

DESK EXERCISES 

     1.   Any series of non-blank characters can be assigned to a
          user variable.

                            True/False



     2.   How can you insert a space into a user variable?





     3.   What utility can be used to display the contents of a
          user variable to standard output?





     4.   The backslash (\) character is used to remove the special
          meaning of some characters.

                            True/False




     5.   What other character can be used to prevent the shell
          from doing the substitution?





     6.   Double quote marks will prevent the shell from making
          the substitution.

                            True/False



                     Continue on the next page
     7.   What do the following shell variables do?


          HOME





          IFS




          MAIL





          MAILPATH





          MAILCHECK





          PATH





          PS1





          PS2






                     Continue on the next page
     8.   What is the command to create a read-only user variable?






     9.   What is the read-only shell variable that represents the
          calling program?






     10.  What do $1,$2,...,$9 represent?






     11.  What BourneShell variable represents all of the command
          line arguments?






     12.  What does the shift command do?






     13.  What is displayed when you enter set with no arguments?













                     Continue on the next page
COMPUTER EXERCISES 


     14.  Login to the Multimax (domax1) using the username and
          password given to you by the instructor.





     15.  Create a subdirectory called sub_dir.





     16.  Modify your .profile to include the following:


          a) Change the home directory to sub_dir

          b) Set the internal-field separator to a comma

          c) Have mail messages saved into mail1.

          d) Set the PATH to look for programs in the following  
             directories:

               $HOME/bin
               /bin
               /usr/bin

          e) Change the prompt to reflect the name of the system

          f) Change the secondary prompt to 'More?'





     17.  Execute the .profile
          Enter $. .profile




     18.  Verify that the changes are correct. If you have extra
           time go to the next page.



                    Extra Mile on the next page
Extra Mile


Change the .profile file so the date/time and a list of all
users that are currently logged in will be displayed on your
monitor screen automatically when you login.
                               NOTES
ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ
3.  POSITIONAL PARAMETERS


A BourneShell script can also read in command-line arguments.  
The first argument is referred to as $1, the second is $2, and so
on.  Command-line arguments are referred to as positional
parameters.

Let's look at an example BourneShell script to see how these are
used.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $cat neat_shell                                               ³
³ echo $1 $2 $3                                                 ³
³ echo $0 is the name of the shell script                       ³
³ echo "There were $# arguments."                               ³
³ echo $*                                                       ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

Insure that the BourneShell script is executable by issuing this
command:

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³    $chmod a+x neat_shell                                      ³
³    $                                                          ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

Now, if we type the name of the BourneShell script with no
arguments, we get the following results.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $neat_shell                                                   ³
³                                                               ³
³ neat_shell is the name of the shell script                    ³
³ There were 0 arguments.                                       ³
³                                                               ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

In this sample session, there were no arguments given so none were
printed.  $0 is the positional parameter that refers to the name
of the script.  Since there were no arguments given with this
invocation of neat_shell, there were zero arguments listed.      
               
3.1  Reading Input Into a Shell Variable

The BourneShell script can read user input from standard input.  
The read command will read one line from standard input and assign
the line to one or more variables.  The following example shows how
this works.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $cat read_script                                              ³
³ echo "Please enter a string of your choice"                   ³
³ read a                                                        ³
³ echo $a                                                       ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

This simple script will read one line from standard input
(keyboard) and assign it to the variable a.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $read_script                                                  ³
³ Please enter a string of your choice                          ³
³ Here it is                                                    ³
³ Here it is                                                    ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

The line read from standard input can also be assigned to several
variables as shown in the following example.


Sample Session:
                                                  
ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $cat reads                                                    ³
³ echo "Please enter three strings"                             ³
³ read a b c                                                    ³
³ echo $a $b $c                                                 ³
³ echo $c                                                       ³
³ echo $b                                                       ³
³ echo $a                                                       ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

This time, we will turn on the trace mechanism and follow the
execution of this BourneShell script.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $sh -x reads                                                  ³
³ + echo Please enter three strings                             ³
³ Please enter three strings                                    ³
³ + read a b c                                                  ³
³ this is more than three strings                               ³
³ + echo this is more than three strings                        ³
³ this is more than three strings                               ³
³ + echo more than three strings                                ³
³ more than three strings                                       ³
³ + echo is                                                     ³
³ is                                                            ³
³ + echo this                                                   ³
³ this                                                          ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

It is interesting to note that the spaces separate the values for
the variables a,b, and c.  For example, the variable a was assigned
the string this, the variable b was assigned the string is, and the
remainder of the line was assigned to c (including the spaces).

Sample Session:
                                                  
ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $cat read_ex                                                  ³
³ echo  'Enter line: \c'                                        ³
³ read line                                                     ³
³ echo "The line was: $line"                                    ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
                                                                 
In this example, the \c option will suppress the carriage return.
The single quote marks protect the backslash from being interpreted
by the shell.  Also notice that the double quote marks have no
effect on the substitution of the variable line.                 
            
Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $read_ex                                                      ³
³ Enter line: All's well that ends well                         ³
³ The line was: All's well that ends well                       ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
3.2  Command Substitution

You can execute a command by enclosing it within two grave accent
marks [these are sometimes called backquotes (`)].  The BourneShell
will replace the command and the grave marks with the output from
the command.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $cat dir                                                      ³
³ dir=`pwd`                                                     ³
³ echo 'You are using the' $dir 'directory'                     ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
                                                                 
NOTE:     The grave marks lean to the left, and the apostrophes 
          lean to the right.  The grave marks enclose the pwd  
          command.                                              
                                                                 
Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $dir                                                          ³
³ You are using the /user0/rharding directory                   ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

The important thing to notice here is that the pwd command was
executed; and the output, /user0/rharding, was then assigned to
the variable dir.

It is not necessary to assign the output of a command to a variable
as shown in the previous example.  The command substitution can
occur directly as shown in the next example.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $cat dir2                                                     ³
³ echo 'You are using the' `pwd` 'directory'                    ³
³ $dir2                                                         ³
³ You are using the /user0/rharding directory                   ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
One final example will show a practical use of command
substitution.  This BourneShell script will use the date command
to provide the date in a useful format.

The normal output from the date command looks like the following.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $date                                                         ³
³ Wed Sep 12 18:02:05 MDT 1990                                  ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

Here's a BourneShell script that rearranges the output into a more
useable format.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $cat dateset                                                  ³
³ set `date`                                                    ³
³ echo $*                                                       ³
³ echo                                                          ³
³ echo 'Argument 1:' $1                                         ³
³ echo 'Argument 2:' $2                                         ³
³ echo 'Argument 3:' $3                                         ³
³ echo 'Argument 4:' $4                                         ³
³ echo                                                          ³
³ echo $2 $3, $6                                                ³
³ $dateset                                                      ³
³ Wed Sep 12 18:02:05 MDT 1990                                  ³
³                                                               ³
³ Argument 1: Wed                                               ³
³ Argument 2: Sep                                               ³
³ Argument 3: 12                                                ³
³ Argument 4: 18:02:05                                          ³
³                                                               ³
³ Sep 12, 1990                                                  ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ

The first command in the BourneShell script dateset uses the grave
accent marks to set the command-line argument variables to the
output of the date command.  The next commands show the first four
of these argument variables.  The final command displays the
arguments in a different order that could be useful in a report or
a letter.
3.3  Comments in BourneShell Scripts


Comments can be inserted into the BourneShell script by beginning
each comment line with the pound symbol (#) or a colon (:).  All
characters after the comment character will be ignored by the
shell.  The only exception to this rule is that the first character
of the first line must not be a pound symbol; if the first
character is a pound sign, the BourneShell tries to execute the
script as if it was written in CShell syntax.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $cat com_sub                                                  ³
³   #   The first line sets your present working directory      ³
³ #     to the variable 'directory'                             ³
³ directory=`pwd`                                               ³
³ #     The second line sets the date to the variable 'when'    ³
³ when=`date`                                                   ³
³ :     The third line will echo on the screen                  ³
³ echo "You are in $directory on $when"                         ³
³ :     You could have said echo :                              ³
³ :        "You are in `pwd` on `date`"                         ³
³ :     to have a one line program                              ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
                                                               
3.4  BourneShell Environment - Exporting Variables

Within a process, you can declare, initialize, read, and modify
variables.  The variable is local to that process.  When a process
forks a child process, the parent process does not automatically
pass the value of the variable to the child process.

Here is an example of the variables not being exported.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $cat no_export                                                ³
³ car=mercedes       # set the variable                         ³
³ echo $0 $car $$    # $0 = name of file executed               ³
³                    # $car =value of variable car              ³
³                    # $$ = PID number (process id)             ³
³ inner              # execute another BourneShell script       ³
³ echo $0 $car $$    # display same as above                    ³
³ $cat inner                                                    ³
³ echo $0 $car $$    # display variables for this process       ³
³ $chmod a+x no_export                                          ³
³ $chmod a+x inner                                              ³
³ $no_export                                                    ³
³ no_export mercedes 4790                                       ³
³ inner 4792                                                    ³
³ no_export mercedes 4790                                       ³
³ $                                                             ³
ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
 
When no_export was executed, it, of course, assigned a value of
mercedes to the variable car and printed it out.  The call to inner
created a child process.  Its PID is 4792, while the parent PID is
4790.  Notice, when inner tried to print the value of car, it
printed nothing.  The reason is because the value of car was not
passed by the parent.
Can the value be passed from parent to child process?  Yes, by
using the export command.  Let's look at an example.

Sample Session:

ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
³ $cat export_it                                                ³
³ car=mercedes                                                  ³
³ export car                                                    ³
³ echo $0 $car $$                                               ³
³ inner1                                                        ³
³ echo $0 $car $$                                               ³
³ $cat inner1                                                   ³
³ echo $0 $car $$                                               ³
³ car=chevy                                                     ³
³ echo $0 $car $$                       

TUCoPS is optimized to look best in Firefox® on a widescreen monitor (1440x900 or better).
Site design & layout copyright © 1986-2024 AOH