You are on page 1of 9

Guide to UNIX Using Linux 4th Edition

Palmer Solutions Manual


Visit to download the full and correct content document: https://testbankdeal.com/dow
nload/guide-to-unix-using-linux-4th-edition-palmer-solutions-manual/
Guide to UNIX Using Linux Fourth Edition
Chapter 6 Solutions

Answers to the Chapter 6 Review Questions

1. Your organization routinely uses scripts, but as some employees have left, there are scripts that
contain only command lines and no one is certain of their purpose. What steps can be taken to
ensure a way for others to know the purpose of a script?
Answer: c. Require that script writers place comment lines inside the scripts using the #
symbol to begin each comment line.

2. Which of the following shells enables the use of scripts? (Choose all that apply.)
Answer: a. Bash, b. csh, and d. zsh

3. You frequently use the command ls -a and want to save time by just entering l to do the same
thing. Which of the following commands enables you to set your system to view hidden files by
only entering l?
Answer: d. alias l=”ls -a”

4. You have written a script, but when you run it there is an error. Which of the following commands
can you use to debug your script? (Choose all that apply.)
Answer: b. sh -v and d. sh -x

5. You have written a shell program that creates four temporary files. Which of the following
commands can you use to remove these files when the script has completed its work?
Answer: a. trap

6. Which of the following commands works well for menus used in a script? (Choose all that apply.)
Answer: b. case

7. You are currently in the source directory, which is the new directory you have just created for
storing and running your scripts. You want to make certain that the source directory is in your
default path. Which of the following commands enables you to view the current default path
settings?
Answer: d. echo $PATH

8. You have created a script for use by your entire department in a commonly accessed directory.
Only you are able to run the script, which works perfectly. Which of the following is likely to be
the problem?
Answer: b. You did not give all users in your department execute permission for that
script.

9. Your current working directory contains a series of files that start with the word “account”
combined with a, b, c, d, and e, such as accounta, accountb, and so on. Which of the following
commands enables you to view the contents of all of these files? (Choose all that apply.)
Answer: c. more account[a,b,c,d,e]

10. For which of the following logic structures used within a script is fi the final line for that logic
structure? (Choose all that apply.)
Answer: d. if

11. Which of the following are examples of arithmetic or relational operators? (Choose all that apply.)
Answer: a. !, b. <, c. %, and d. *

1
12. You have created a series of scripts that use the same environment variables. However, when you
run these scripts, some of them do not seem to recognize the environment variables you have set.
What is the problem?
Answer: a. You need to use the export command so these variables have global use.

13. You have spent the last two hours creating a report in a file and afterwards you use cat to create a
new file. Unfortunately the new file name you used was the same as the name you used for the
report, and now your report is gone. What should you do next time to prevent this from
happening?
Answer: b. Enter the command, set -o noclobber before you start.

14. You have remotely logged into a computer running UNIX or Linux, but you are not certain about
which operating system you are using. However, when you display the contents of the
______________ variable it shows which operating system you are using.
Answer: d. OSTYPE

15. What command can you use to view the environment and configuration variables already
configured on your system?
Answer: c. printenv

16. Which of the following are valid expressions? (Choose all that apply.)
Answer: a. let x=5*9, b. let x=y+10, c. let m=12/4, and d. let r=128-80

17. When you type for wood maple spruce oak pine at the command line and then press Enter, what
should you type next at the > prompt?
Answer: a. do

18. You want to store a long listing of your files in a variable called myfiles. Which of the following
commands enables you to do this?
Answer: c. myfiles=`ls –l`

19. What error is in the following script code?

case “selection” in
“i.”) ./listscript ;;
“ii”) ./numberscript ;;
“iii”) ./findscript ;;
esac

Answer: b. There should be a dollar sign in front of selection, as in “$selection”

20. You are working with a colleague on a script called value that updates several files. You want to
test the script, but not update the files. Which of the following commands can you use?
Answer: d. sh -n value

21. You only have to enter the name of a script to have it run, such as entering myscript. What setting
enables you to do this?
Answer: d. You have placed the directory from which you run the scripts in your PATH
variable.

22. What would you expect to find in the HOME environment variable?
Answer: The HOME environment variable identifies the path name for the user’s home
directory.

23. What is the difference between a compiler and an interpreter?

2
Answer: A compiler takes code from a program language, such as C or C++ and converts
the code into machine language instructions in a separate file to be executed later. An
interpreter takes commands or code, such as from a script and translates them into
executable instructions that run on the spot.

24. What command would you use to place the cursor in row 10 and column 15 on the screen or in a
terminal window?
Answer: Use the command tput cup 10 15.

25. What is the purpose of a login script?


Answer: The login script runs each time you log into your account and can include
commands, such as aliases and the set -o noclobber command, that take effect as soon as
the script runs and that last for the duration of the login session. (unless they are manually
changed by the user later during the login session).

Hands-On Projects Tips and Solutions for Chapter 6


Project 6-1
In this project, students view a listing of their environment variables.

In Step 2, a sampling of variables that students might record are:

▪ HOSTNAME
▪ SHELL
▪ TERM
▪ HISTSIZE
▪ USER
▪ SESSION_MANAGER
▪ MAIL
▪ PATH
▪ INPUTRC
▪ PWD
▪ LANG
▪ HOME
▪ LOGNAME.

Project 6-2
This project enables students to learn how to assign a shell variable, how to view the contents of a
variable, how to use double quotes and single quotes when manipulating shell variables, and how to
use backquotes to execute a command and store the result in a shell variable.

Project 6-3
In this project, students practice using the let command with constants and with a shell script variable
so they become familiar with this capability before they build more advanced scripts in later projects in
this chapter.

At this point, if you have students who are out of practice using basic mathematical and algebraic
concepts, you might spend a classroom session reviewing variables, expressions, and so on.

3
Project 6-4
In this project, students learn to export a shell variable to make it universally accessible as an
environment variable.

Project 6-5
For this project, students learn how to determine the contents of the PATH environment variable and
then how to add the current working directory to the PATH variable so they can execute scripts
without using the ./ characters.

Project 6-6
In this project, students create a short script to demonstrate sequential logic and to get additional
practice in using the let command as well as building expressions using constants, variables, and
arithmetic operators.

Project 6-7
Students use if statement decision logic in this project. In the first set of steps they create a script using
a basic if statement and in the second set of steps they modify their script to include an if statement
nested within an if statement.

Project 6-8
In this project, students first create a shell script containing a for loop that prints the names of six users
on individual lines. Next, students learn how to execute the same for loop logic directly from the
command line.

In Step 4 of the second set of steps, students should see the following list displayed to the screen:
▪ john
▪ ellen
▪ tom
▪ becky
▪ eli
▪ jill

Project 6-9
For this project, students practice using the brackets wildcard format to run a for loop.

Project 6-10
For this project, students create two scripts to practice using the while statement. The first script uses a
simple while statement to guess the favorite color and the second script is a more complex data input
form.

Project 6-11
This project enables students to practice using case logic in a simple script. They will learn much more
about using case logic in upcoming projects.

4
Project 6-12
In this project, students first practice the tput command from the command line to get an instant idea of
how the command works. In the second set of steps, students create a simple menu that runs via a shell
script.

Project 6-13
For this project, students use the sh -u and sh -v commands to learn about debugging. By now, students
will have likely made some mistakes in creating scripts and will understand the importance of these
commands. Because shell scripts are now getting more complex, it is important for students to have
this tool available from this point on.

Project 6-14
In this project, students learn how to create an alias. Consider using this project as an opportunity to
discuss aliases that you like to use in your work or that you have incorporated into a login script.

Project 6-15
This project is the first in a series of projects in which students create a telephone list application that
simulates one that might be used in an organization. In the first set of steps, students make sure they
have a source subdirectory in which to store their application files. Next, they create a beginning menu
application.

Project 6-16
Students will need a data file with some practice data already in it for testing their application as they
go along. In this project, they delete the former versions of the corp_phones files created for practice in
Chapter 4 to make sure that they are starting with known data. Then they create a new corp_phones file
in their source directory. This project also helps ensure that students begin with some familiarity of the
data. Note that to ensure they start fresh, there are some differences between the contents of this file
and the files they created in Chapter 4.

It is common for application developers to use practice data files with a few known data entries when
they develop an application. Consider holding a discussion about why these files are important and
discuss practice data files you may have used when you have developed applications for an
organization.

Project 6-17
In this project, students edit the phmenu script so that it can call applications..

Project 6-18
For this project, students again edit the phmenu script to be able to print raw data to view for
verification of the data.

Project 6-19
For this project, students create and test the phlist1 script to display a listing of telephone number
information. This script can be run from the phmenu script as well.

5
Project 6-20
In this project, students create the phoneadd script from which to add new records to the corp_phones
file.

Discovery Exercises
1. Use two different commands to display the contents of the HOME variable

Answer: Type printenv HOME and press Enter. Also, type echo $HOME and press Enter.

2. Assign the variable t the value of 20. Next, assign the variable s the value of t+30. Finally, display
the contents of t and s to verify you have correctly defined these variables.
Answer: Type t=20 and press Enter. Next, type let s=t+30 and press Enter. To verify the
contents of t type echo $t and press Enter. Next to verify the contents of s type echo $s
and press Enter.

3. Make the s variable you assigned in Exercise 2 an environment variable and use the command to
verify it is recognized as an environment variable.

Answer: Type export s and press Enter. Next, type printenv or printenv s and press Enter
to verify that s is now recognized as an environment variable.

4. Switch to your source directory. Display the contents of the PATH variable. Next, use the
command to add your current working directory to the PATH variable.

Answer: Type cd source and press Enter. Next, type echo $PATH or printenv PATH and
press Enter. Finally, type PATH=$PATH:. and press Enter.

5. After completing Exercise 4, run the phmenu program in the easiest way.

Answer: Because the source directory is now in the path, you simply type phmenu and
press Enter.

6. Create a variable called iam and assign the results of the whoami command to it. Display the
contents of the variable to verify your results.

Answer: Type iam=`whoami` and press Enter. Next, type echo $iam and press Enter.

7. Change back to your home directory, if you are not in it. Use the set command to set up your
working environment to prevent you from overwriting a file.

Answer: Type set -o noclobber and press Enter.

8. Create an alias called var that displays your environment variables.

Answer: Type alias var=” printenv” and press Enter.

9. At the command line use a for loop that uses the variable sandwiches and then displays a line at a
time the following sandwiches: chicken, ham, hummus, tomato.

Answer: Type the following at the command line:


for sandwiches in chicken ham hummus tomato <Enter>

6
>do <Enter>
>echo $sandwiches <Enter>
>done <Enter>

10. Create a script that uses case logic to have someone guess your favorite sandwich, such as tuna.

Answer: The lines of code in the script should be, for example:
echo -n "Guess my favorite sandwich: "
read guess
case “$guess” in
“tuna”) echo “Tuna is my favorite sandwich” ;;
* ) echo “Nope, actually I like tuna” ;;
esac

11. Display the contents of .bashrc file. Next, use the vi editor to edit that file and put in an alias so
that every time you type list you see a long file listing of a directory.

Answer: Make sure you are in your home directory (enter pwd and then enter cd if you
are not in your home directory). Type less .bashrc and press Enter (or students can use
more or cat) to see the contents of the .bashrc file. Next, use vi or Emacs to place the line
alias list=”ls –l” under the # User specific aliases and functions section in the file.

12. Use a command to simulate how you would troubleshoot a problem with the sandwich script you
created in Exercise 10.

Answer: Type sh -x sandwich or sh -v sandwich and press Enter.

13. What is wrong with the following lines of code?


While [ “$value” = “100” ; do
Echo “That’s a large number.” read value
fi

Answer: 1) there should be a closing bracket after “100”, 2) Echo should not have an
initial capital letter, 3) there should be a semicolon on the second line to separate
number.” and read value, and the third line should have done instead of fi.

14. Use the let command to store the value 1024 in the variable ram. Display the contents of ram.

Answer: Type let ram=1024 and press Enter. Next, type echo $ram and press Enter.

15. Temporarily change your home directory environment variable to /home and then use one
command to go to your home directory. Change the home directory environment variable back to
your regular home directory and switch to it.

Answer: Type HOME=”/home” and press Enter. Next, type cd and press Enter. To go
back to the default, type HOME=”/home/username” and press Enter. Next, type cd and
press Enter

16. Use the tput command to clear the screen and then to place the cursor in row 7, column 22:

Answer: Type tput clear and press Enter. Next type tput cup 7 22 and press Enter.

17. Write a script that creates the following menu:


Soup Menu
==========

7
(t)omato
(b)ean
(s)quash
Select a soup … (q) to quit

Answer: Here is some example code:

loop=y
while [ "$loop" = y ]
do
clear
tput cup 3 12; echo "Soup Menu"
tput cup 4 12; echo "========="
tput cup 6 9; echo "(t)omato"
tput cup 7 9; echo "(b)bean”"
tput cup 8 9; echo "(s)squash"
tput cup 10 9; echo "Select a soup … (q) to quit”
tput cup 11 9
read choice || continue
done

18. List all of the signal numbers and designations for the trap command. What is the designation for
signal 31?

Answer: Type trap -l and press Enter. The designation for signal 31 is SIGSYS.

19. Modify your script from Exercise 17 so that there is a beep when the menu is ready to take the
user’s input.

Answer: Use the line near the end as follows:


tput cup 10 9; echo -e "Select a soup … (q) to quit \a”

20. Is there a command that you can use to prevent shell variables from being assigned new values? If
so, what is it?

Answer: Yes. Use the readonly command in the Bash shell.

You might also like