Chapter 4 - The UNIX Shells
Quiz
1. Can you change your default shell?
- Most versions of UNIX provide the chsh command to allow a
user to change his or her default shell. If this command is not available,
the system administrator must modify your entry in the system password
file to change your default (login) shell.
2. What UNIX command is used to change your current directory?
- Use the cd command to change your current directory to a new
location.
3. How can you enter commands that are longer than one line?
- You can continue a shell command on a new line by typing a backslash
( \ ) as the last character of a line. When you hit RETURN or ENTER,
the shell will prompt you to continue the command line.
4. What is the difference between a built-in command and a utility?
- A built-in command is part of the shell itself. A utility is an executable
program that is installed (usually in a system directory such as /bin
) which the shell executes.
5. How can you make a script executable?
- You can make a script executable by setting the execute bit in the
file permission for the file. To make it executable to all users, including
the owner and others in the same group as the owner, you would set the
execute bit for all three. A script that can be read, written, and executed
by the owner, and only read and executed by all other users would have
a permission mode of 755.
6. What is the strange term that is sometimes given to filename substitution?
- Filename substitution is also called ìglobbing.î
7. Describe a common use for command substitution.
- There are many correct answers to this question. An example would
be using ì*.cî to refer to all C program source files in the current
directory (e.g. ls ñl *.c ).
8. Describe the meaning of the terms parent shell, child shell,
and subshell.
- Any process executing a shell can itself execute another shell that
will perform some task and return to the original shell. This is similar
to the parent process and child process paradigm. The first shell is
called the parent to the shell it executes (creates). The new shell
is the child to the original shell. The term subshell is just another
way of describing the child shell.
9. How do you think the kill command got its name?
- The kill command is somewhat misleadingly named since what
it really does is send a signal to a process. Any signal can be specified
but by default, the signal that is sent is a SIGTERM signal which will
cause the process to terminate. It was probably named kill because
killing a process is the most common use of the command, so in many
cases the name makes perfect sense. Consider the case, however, where
you send a SIGUSR1 (user defined) signal to a running process because
it will trap the signal, do some special processing, and then return
to what it was doing before the signal was sent. In this case, you really
arenít killing the process, you are simply sending it a message.
10. Describe a way to override a standard utility.
- The most common way to override a standard utility is to put an executable
file of the same name in a directory in your search path ($PATH) which
is ahead of the directory where the standard utility is located. In
some shells, another way would be to define an alias of the same name.
11. What is a good umask value, and why?
- A umask value of 022 will mask off the write permission bits of any
file you create, thus by default not allowing write access to the file
by any user other than yourself (the creator of the file). For the ultra
paranoid, a umask of 077 might be preferable which masks off all
access permission to the file by any other user.
Exercises
4.1 Write a script that prints the current date, your user name, and the
name of your login shell. [level: easy]
- This script will use echo (or print in the Korn shell)
to display the variables $USER and $SHELL which contain the userís login
name and current shell. The date command will also be executed
to print out the current date.
4.2 Experiment with the exec command by writing a series of three
shell scripts called ìa.sh,î ìb.sh,î and ìc.shî, each of which displays
its name, executes ps, and then execs the next script in the
sequence. Observe what happens when you start the first script by executing
exec a.sh. [level: medium]
- A shell can print out $0 to show the name of the command that was
used to start it. Output from the ps command will show it running.
After executing the next script with exec, the student should notice
in the next ps output that the new script name is present but
the old one is gone. This is because exec replaces the current program
with the new program when it runs it. At each execution of a new script,
the old one is replaced and only the new one will show up as currently
running. If you execute your first script with exec from your shell,
then your shell itself is replaced when the first script runs. Then
when the last script in the chain terminates, you may find yourself
logged out of the system (or if youíre running in a terminal window,
the terminal window will exit)!
4.3 Why is the file that is created in the following session unaffected
by the umask value? [level: medium]
- The file is unaffected because the umask value only affects the permissions
of a file at the time it is created. Since this file was already in
existence when the umask value was changed, its permissions were already
set. The subsequent date command merely overwrote an existing
file.
4.4 Write a script that creates three background processes, waits for them
all to complete, and then displays a simple message. [level: medium]
- Such a script will fork off three programs and then execute a wait
command. This will cause the shell to sleep until all three programs
have terminated. The next command after the wait command could
be echo with a simple message.
Project
Compare and contrast the UNIX shell features with the graphical shells available
on Windows. Which do you think is better? [level: medium]
- Shells fundamentally provide the same function, to allow easy access
to files and utilities including the ability to execute, rename, delete,
or open them. The main issues for this discussion would be whether someone
prefers command line access to these functions or a graphical interface.
|