Harley Hahn's Guide to
|
A Personal Note
Chapters...
Command
INSTRUCTOR |
Exercises and Answers for Chapter 12... Using the Shell: Variables and Options Review Question #1: What is the difference between an interactive shell and a non-interactive shell? Answer Interactive shell: A shell that provides the interface for a user working at a terminal. Non-interactive shell: A shell that runs independently of a person, usually a shell that is running a shell script. Review Question #2: The environment is a table of variables available to the shell and to any program started by that shell. What type of variables are stored within the environment? Give three examples. What type of variables are not part of the environment? Answer Environment variables are stored in the environment. Thus, they are available to programs started by the shell. Examples are HOME, PATH, SHELL, TERM and USER. See Figure 12-2 (page 263) for more examples. Shell variables are not part of the environment. Review Question #3: With the Bourne shell family (Bash, Korn shell), what command do you use to make a shell variable part of the environment? Answer The export command. Review Question #4: How do you display the values of all your environment variables? Your shell variables? A single variable? Answer To display all your environment variables use env or printenv. To display all your shell variables, use set. To display the value of a single variable, use echo followed by a $ (dollar) character and the name of the variable, for example: echo $SHELL Review Question #5: Explain the terms "machine-readable" and "human-readable". Answer Machine-readable: describes output that is formatted so as to be suitable for processing by another program. Such output may be difficult for a person to read. Human-readable: describes output that is designed to be particularly easy for a person to read. Applying Your Knowledge #1: The environment variable USER contains the name of the current userid. Show three different ways to display the value of this variable. Answer To display the value of the USER environment variable, use one of the following commands:
env
Applying Your Knowledge #2: Create an environment variable named SPORT and give it the value "surfing". Display the value of the variable. Start a new shell. Display the variable again to show that it was passed to the new shell as part of the environment. Change the value of SPORT to "running", and display the new value. Now quit the shell and return to the original shell. Display the value of SPORT. What do you see and why? Answer Bash or Korn Shell:
export SPORT=surfing
Tcsh or C-Shell:
setenv SPORT surfing
In both cases, after returning to the original shell, SPORT has the value "surfing" as the change was not passed back from the child shell. Applying Your Knowledge #3: Within the Bourne shell family (Bash, Korn shell), the ignoreeof option tells the shell not to log you out when you press ^D (the eof key). Start either Bash or a Korn shell. Check if ignoreeof is turned on or off. If it is off, turn it on. Press ^D to confirm that you do not log out. Then turn ignoreeof off. Press ^D again to confirm that you do indeed log out. Answer The commands are:
set -o
For Further Thought #1: Environment variables are not true global variables, because changes made by a child process are not propagated back to the parent. Suppose this was not the case. Show how a hacker might use this loophole to cause trouble on a multiuser system on which he has an account. Answer Here is one scenario. A hacker offers an interesting program for free to other users on the local system. When someone runs the program, it has the side effect of changing the value of their PATH variable, so that one of the hacker's directories comes at the beginning of the path. In this directory, the hacker has put his own version of common programs (such as date, ls, and so on) that do whatever he wants. For Further Thought #2: The C-Shell family has a clear separation between environment variables and shell variables. The Bourne shell family is not so clear, because a newly created variable is, by default, a shell variable. If it is exported, it becomes both a local and an environment variable. What are the advantages and disadvantages of each system? Which system do you think is better, and why? Why do you think the Bourne shell family has such a confusing way of dealing with variables? Answer C-Shell family: Strict separation between the two types of variables makes them easier to use and understand. By convention, environment variables have uppercase names; shell variables have lowercase names. Bourne shell family: The blurring of the two types of variables leads to confusion. There are no environment-only variables. All variables have uppercase names, so you can't tell what type a variable is by looking at it. There is no easy way to tell if a variable is only a shell variable and not an environment variable. The Bourne shell family uses this system to maintain backward compatibly with earlier shells and existing shell scripts. Exercises: Introduction | Chapter list
© All contents Copyright 2024, Harley Hahn
|