Please contact or Mt Xia for assistance with all your shell programming needs.
2. USER, SHELL, AND READ-ONLY SHELL VARIABLESThe 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 VariablesIt 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 VariablesThe 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 HOMEThe 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 IFSThis 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 MAILThe 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 MAILPATHThis 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 MAILCHECKThis 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 PATHThis 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 PS1This 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 PS2This 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? ".