-

Bourne Shell: Chapter 2b

-

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


  • 2. USER, SHELL, AND READ-ONLY SHELL VARIABLES Go to the bottom, first,previous,next, last section, table of contents.

    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 $

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

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