|
Chapter 2: The Bourne Shell User Test Your Thinking |
![]() |
set
You can save the results and put them in a file, and then modify the
file so that those values are set manually when you execute this file.
A great way to understand your environment values are is to study the
file. Save this program, because you can execute it whenever you need
to reset your environment. Just use the "." dot command to execute the
file.
Solution:
You can do this by entering the set command and saving the results into a file
set > my.out
You should just get the command line prompt back.
Your answer depends on your version of UNIX, but if you look at the file created in Solaris x86 you will see the following:
HOME=/export/home/myid HZ=100 IFS= LC_COLLATE=en_US LC_CTYPE=en_US LC_MESSAGES=C LC_MONETARY=en_US LC_NUMERIC=en_US LC_TIME=en_US LOGNAME=myid MAIL=/var/mail/myid MAILCHECK=600 OPTIND=1 PATH=/usr/bin:/usr/ucb:/usr/local/bin:. PS1=$ PS2=> PWD=/export/home/myid/web/chap2 SHELL=/bin/sh TERM=ansi TZ=US/Central _=/usr/bin/sh
You will need to edit the file because some of the lines will not be relevant in the shell and are only temporary values created by the execution of your last command. You should remove the following lines from your new file
HZ=100 IFS= OPTIND=1
The file that you would use to set your environment would then be the following:
#!/bin/sh # This is my program to set the environment HOME=/export/home/myid LC_COLLATE=en_US LC_CTYPE=en_US LC_MESSAGES=C LC_MONETARY=en_US LC_NUMERIC=en_US LC_TIME=en_US LOGNAME=myid MAIL=/var/mail/myid MAILCHECK=600 PATH=/usr/bin:/usr/ucb:/usr/local/bin:. PS1=$ PS2=> PWD=/export/home/myid/web/chap2 SHELL=/bin/sh TERM=ansi TZ=US/Central
Log in as root. Repeat Project 1.
Solution:
First you need to login as root. You can get the results you want by entering the set command and saving the results into a file
set > root.out
You should just get the command line prompt back.
Your answer depends on the version of UNIX you are using, but if you look at the file created in Solaris x86 you will see something similar to the following:
HOME=/ HZ=100 IFS= LOGNAME=root MAILCHECK=600 OPTIND=1 PATH=/usr/sbin:/usr/bin:/etc:/usr/local/bin PS1=# PS2=> SHELL=/sbin/sh TERM=vt100 TZ=US/Central
You should note that if you login as root from the console in Solaris x86, while you are running Sun's Open Windows, you will get many more lines of output then the above. The above lines are samples and your will need to examine your own output. You will need to edit the resulting file because some of the lines will not be relevant in the shell and are only temporary values created by the execution of your last command. You should remove the following lines from the above sample file
HZ=100 IFS= OPTIND=1
The file that you would use to set your environment would then be the following:
HOME=/ LOGNAME=root MAILCHECK=600 PATH=/usr/sbin:/usr/bin:/etc:/usr/local/bin PS1=# PS2=> SHELL=/sbin/sh TERM=vt100 TZ=US/Central
Write a program that compares the two programs that resulted from
the previous two projects in this section. You might use the command:
diff
Analyze this program to understand the differences in the environment between logging in as a regular user and logging in as root.
Solution:
The program to compare the two would contain the following:
diff my.out root.out > out.diff
The resulting file will look something like the following:
1,9c1,2 < HOME=/export/home/myid < LC_COLLATE=en_US < LC_CTYPE=en_US < LC_MESSAGES=C < LC_MONETARY=en_US < LC_NUMERIC=en_US < LC_TIME=en_US < LOGNAME=myid < MAIL=/var/mail/myid --- > HOME=/ > LOGNAME=root 11,12c4,5 < PATH=/usr/bin:/usr/ucb:/usr/local/bin:. < PS1=$ --- > PATH=/usr/sbin:/usr/bin:/etc:/usr/local/bin > PS1=# 14,16c7,8 < PWD=/export/home/myid/web/chap2 < SHELL=/bin/sh < TERM=ansi --- > SHELL=/sbin/sh ÿ TERM=vt100
The Results can be summarized n a table format as follows as follows:
Regular User |
Root |
<pre>HOME=/export/home/myid |
<pre>HOME=/ |
<pre>PATH=/usr/bin:/usr/ucb:/usr/local/bin:.</pre> |
<pre>PATH=/usr/sbin:/usr/bin:/etc:/usr/local/bin</pre> |
<pre>PWD=/export/home/myid/web/chap2 |
<pre>SHELL=/sbin/sh |
You can reduce the differences by looking at the following table. Those values that are not necessary for the discussion are eliminated. This will summarize the differences between running as a regular user and running as root.
Regular User |
Root |
Discussion |
HOME=/export/home/myid |
HOME=/ |
The home directory for root is / whereas the home directory for the regular user is /export/home/myid |
LOGNAME=myid |
LOGNAME=root |
The id that was used for the login is myid for the regular user and root for the root user. You should be aware that if you do an su to root, the value of LOGNAME may not change. This is normally not a problem, but if you are using a program which test for the value of $LOGNAME it will give the wrong results. |
PATH=/usr/bin:/usr/ucb:/usr/local/bin:. |
PATH=/usr/sbin:/usr/bin:/etc:/usr/local/bin |
The search path for the regular user includes /usr/ucb, which includes some programs that are carried over from Berkeley UNIX. The search path for root includes /etc and /usr/sbin since these directories includes administrative programs that most users won't need access to. |
PS1=$ |
PS1=# |
The prompt for a regular user is $, whereas the prompt for root is # |
SHELL=/bin/sh |
SHELL=/sbin/sh |
The shell for a regular user is /bin/sh, whereas the shell for root is /sbin/sh. This is because the /sbin/sh allows some additional features for root that a normal user don't have. |
Write a program that determines whether you are running the program as a regular user or root, and then set the environment accordingly. Use the programs you created in Projects 1 and 2 to set the proper environments.
Solution:
You can do this by running the following program:
#!/bin/sh # My test program if test $LOGNAME = root then echo "I am logged in as root" else echo "I am not logged in as root" fi
Now it's time to be a detective and check out the regular user's environment. Take Project 1 and determine which environmental settings are set up by:
Solution:
You should note that the origin of some variables may not be clear. Generally those values that you cannot attribute to a file being executed are being set as default values by the shell. The purpose of this exercise is to help you determine, as much as possible, the origin of your environment settings.
/etc/profile
PATH=/usr/bin:/usr/ucb:/usr/local/bin:. TERM=ansi
.profile
Nothing in this particular example is attributable to the .profile file. You should examine your .profile file to see if any parameters there are set in the environment
Bourne Shell
The following settings are assumed to be from the Bourne shell since they cannot be found to be set in any of the standard environment files we have discussed.
IFS= LOGNAME=myid MAIL=/var/mail/myid MAILCHECK=600 PS1=$ PS2=> SHELL=/bin/sh HOME=/export/home/myid HZ=100
Other sources
The file /etc/TIMEZONE gives the following values
LC_COLLATE=en_US LC_CTYPE=en_US LC_MESSAGES=C LC_MONETARY=en_US LC_NUMERIC=en_US LC_TIME=en_US TZ=US/Central
The following are produced by other sources and are generally changed from one execution of a program to another.
OPTIND=1 PWD=/export/home/myid/web/chap2 _=/usr/bin/sh
Continuing on as a detective, you need to look at root's environment.
Take Project 1 and determine which environmental settings are set up by:
Solution:
PATH=/usr/sbin:/usr/bin:/etc:/usr/local/bin TERM=vt100
.profile
Nothing in this particular example is attributable to the .profile file. You should examine your .profile file to see if any parameters there are set in the environment
Bourne Shell
The following settings are assumed to be from the Bourne shell since they cannot be found to be set in any of the standard environment files we have discussed.
HOME=/ LOGNAME=root MAILCHECK=600 SHELL=/sbin/sh IFS= PS1=# PS2=>
Other sources
The file /etc/TIMEZONE gives the following values
TZ=US/Central
7)Go through the exercises in this chapter and redo them in the Bourne Shell, except this time, leave out the export command. You used this in places such as:
Solution:
export PATH
export PS1
Other exported variables
Then you should:
Determine which commands depend on those variables being set.
Execute them without the variables being set.
Export the values.
Determine what the differences are. Be aware that some variables
do not need to be exported, whereas others do.
8)You have learned about some of the options to sh in this
chapter. Some of those include -x and -n. Look up the following
options for the Bourne Shell. Explain what effects they have on executing
a shell program. Consider the option as it is used in the following:
sh -option myprogram
-e
Solution:
The command will fail as soon as it hits an error in the shell program
-i
Solution:
It sets an interactive mode
-v
Solution:
It prints out the lines of the program as they are read from the program.
Look at other options that might be useful
Solution:
sh -x is a very handy command because the program will print out the commands with their arguments as they are executed. It allows you to examine variable values to make sure they are properly set. It is very useful for debugging your shell commands.
|
© 1999-2000 by Prentice-Hall, Inc. A Pearson Company Distance Learning at Prentice Hall Legal Notice |