Visit our newest sister site!
Hundreds of free aircraft flight manuals
Civilian • Historical • Military • Declassified • FREE!


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-2014 AOH