CPS 444/544: Practice Problems
Note: For any regular expressions given below (shell, filter, or
otherwise), and indicate
one space and one tab, respectively.
(added September 8, 2005)
C programming
- What problem may occur with the following code?
char c;
while ((c = getchar()) != EOF) {
...
}
- Is one of the following assignments incorrect in C? Explain.
struct node *p, *q;
p = malloc (3*sizeof (struct node));
q = (struct node *) malloc (3*sizeof (struct node))
- A program once contained the following:
#include <math.h>
...
y = cos (x);
...
and yet the definition of the cosine function was not found. What happened?
- What's wrong with the following recovery?
printf ("Enter your age:\n");
while (scanf ("%d", &age) < 1)
printf ("Error. Try again:\n");
Essentials, File manipulation, and Korn Shell
- To log off of the Korn shell, you should
- enter EOF
- enter stop
- enter logoff
- enter logout
- enter bye
- To list your . files, enter
- dot
- .
- ls -l
- ls -F
- ls -a
- To list all the files ending in .c or .f, enter
- ls *[cf]
- ls *.[c|f]
- ls *.[cf]
- Which of the following are not shell metacharacters (give all that apply)?
- $
- .
- &
- |
- /
- \
- Give and explain the output of the following command lines:
- $ touch /
- $ touch \/
- $ touch '/'
- Explain the difference between a program and a process.
- Which command indicates which computer you are logged into?
- Which command indicates the name and version of the OS running
on the computer into which you are logged?
- Explain why it is be a good idea to single quote the character to
which you set
a kernel metacharacter using stty (e.g.,
$ stty kill 'a').
- When does the kernel start interpreting its metacharacters?
- When does the shell start interpreting its metacharacters?
- (courtesy exercises 1-1 and 1-2, p. 7 of [KP])
Start with the following environment:
$ stty kill '@'
$ stty erase '#'
$ stty lnext '\'
$ sh
Explain the results of each of the commands in the following transcript:
$ date\@
date@: not found
$ date
Fri Sep 2 09:10:45 EDT 2005
$ #date
Fri Sep 2 09:10:45 EDT 2005
$ \#date
- Assume the pound # and backslash \ characters
serve as the erase and escape kernel metacharacters, respectively,
and that, for each filename pattern-matching
expression, there is at least one file which matches that
expression. Explain the output of each of the following Korn shell command
lines (assume each is entered at the keyboard; something else might
appear on the screen).
- $ \# pwd
- $ ls myfile > thisfile
- $ ps
- $ ls -ld .
- To append output from an executable file pgm to a file
data, enter:
- pgm | data
- pgm > data
- pgm >> data
- In vi, to delete three words forward from the cursor, enter
- d3w
- 3dd
- 3x
- d3f
- To read a file trig.c into vi at the cursor position, enter:
(assume trig.c resides in the directory from which vi was
started and that you are in command mode)
- r trig.c
- :r trig.c
- <esc>r trig.c
- What is a daemon process? Give an example.
- Give 5 hallmarks of Unix.
- What is the difference the between the filename value of
the ENV variable
and the .profile file. When is each sourced?
- Explain the sequence of steps that the system
follows from the
time one enters their password successfully up until the
first prompt for input is displayed.
- Consider the following series of commands and outputs:
$ cat .profile
ENV=".myenv"; export ENV
ADDENV=".kshrc"
export PS1="Go ahead $ "
EXINIT="showmode showmatch ruler"
. $ADDENV
$ cat .kshrc
. $HOME/.profile
$ cat .myenv
Identify the most critical problem above?
- Consider the following series of commands and outputs:
1 $ cat .profile
ENV=".myenv"; export ENV
ADDENV=".kshrc"
export PAGER=less
EXINIT="showmode showmatch ruler"
. $HOME/.kshrc
2 $ cat .kshrc
alias ll=ls -l
3 $ cat .myenv
. $HOME/.kshrc
$ ksh
4 $ echo $PAGER
5
6 $ echo $EXINIT
7
- What is printed on line 5?
- What is printed on line 7?
- Consider the following series of commands and outputs:
1 $ cat .profile
ENV=".kshrc"; export ENV
ADDENV=".myenv"
export PAGER=less
EXINIT="showmode showmatch ruler"
. $HOME/$ADDENV
2 $ cat .kshrc
alias ls=dir
alias cd..=cd ..
alias goup=cd ..
3 $ cat .myenv
alias ll=ls -l
$ ksh
4 $ echo $PAGER
5
6 $ echo $EXINIT
7
- Identify a potential problem above.
- What is printed on line 5?
- What is printed on line 7?
- Consider the following series of commands:
1 $ cat S
2 A=hello
3 export B=world
4 export ENV=""
5 $ . S
6 $ ksh & S
7 $ ksh
8 $ echo $A ()
9
10 $ export A=bye
11 $ echo $B (world)
12
13 $ B=planet
14 $ echo $A (bye)
15
16 $ echo $B (planet)
17
18 $ ksh
19 $ echo $A (bye)
20
21 $ echo $B (planet)
22
23 $ exit
24 $ echo $A (bye)
25
26 $ echo $B (planet)
27
28 $ exit
29 $ echo $A (hello)
30
- What is printed on line 9?
- What is printed on line 12?
- What is printed on line 15?
- What is printed on line 17?
- What is printed on line 20?
- What is printed on line 22?
- What is printed on line 25?
- What is printed on line 27?
- What is printed on line 30?
- Assume your login shell is Korn.
- What does the PATH variable do?
- How would you change the
value of the PATH variable by extending it with $HOME/bin at the
beginning of its current value, in such a way that this new value would be
in effect each time you logged in, and its value would also affect all
descendant processes of your login shell?
- Assume you have an executable file $HOME/bin/ls
and your PATH is modified
as indicated above. Explain what the $ which ls
command line would output
and why.
- The Korn shell. Explain the symbols and the effect (output) of the command
(assume each of the following appears as shown on the screen).
For full credit explain which entity (shell or command) is
interpreting which metacharacter. Draw diagrams if necessary.
- $ echo "## But 10 < 12 ##"
- $ export PS1="What now? "
- $ print "Take the correct $PATH"
- $ echo \$HOME
- $ mv *.c *.cpp
- $ type which
- $ whereis whatis
- $ whereis which
- $ which whereis
- Find an example where ksh and csh differ in their
behavior.
Click here for
an example (do not use any of these points of
departure).
- List and describe succinctly one item from each of the
first three
sections of the Unix Reference manual. Each of these items must be
accessible using the man command on our system.
Do not copy whole pages from the manual. Instead,
phrase the explanations in your own words.
Click here for
an example (do not use any of these entries).
- Do not give the definitions, but for each of the following, state in
which
section (1, 2, or 3) of the Unix Manual you would find it described,
with brief reasons.
- strlen
- bash
- read
- Explain the difference between an absolute and a relative
path.
- Give a directory owned by root in which you have write
permissions.
- Give examples of three top-level subdirectories (i.e.,
children of root /)
and a brief description of the role of each.
- (courtesy exercise 1-4, p. 29 of [KP])
Consider the file junk.
Take one sentence to explain the output of each of the
following command lines (there are 10):
$ ls junk | $ echo junk |
$ ls / | $ echo / |
$ ls | $ echo |
$ ls * | $ echo * |
$ ls '*' | $ echo '*' |
For each of the of the rows above
compare the command line in the first column
to that in the second column.
- (courtesy exercise 1-5, p. 31 of [KP])
Explain why
$ ls >ls.out
causes ls.out to be included in the list of files.
- (courtesy exercise 1-6, p. 31 of [KP])
Explain the output from
wc temp > temp
If you misspell a command name, as in
$ woh >temp
what happens?
- Create a file named -r. Show how you did this.
Now remove the file with the rm command. Show how you
did this.
- (courtesy exercise 2-8, pp. 63 of [KP])
cp doesn't copy subdirectories, it just copies files at the
first level of a hierarchy. What does it do if one of the argument
files is a directory? Is this kind or even sensible? Discuss the
relative merits of three possibilities: i) an option to cp
to descen directories, ii) a separate command rcp (recursive
copy) to do the job, or iii) just having cp copy a directory
recursively when it finds one. What other programs would benefit
from the ability to traverse the directory
tree?
(added September 13, 2005)
- Consider the following session with the Korn shell:
$ pwd
$ /home/cps444-n1.19
$ ls
C/ bin/ text/ wc.c
$ cd C
$ pwd
$ /home/cps444-n1.19/C
$ ls
cat.c myshell.c mine.c
$ cd text
$ pwd
$ /home/cps444-n1.19/text
Give and explain two directories in the user's
CDPATH.
- Is export a shell built-in or a Unix command?
Show how to determine the answer?
- Consider the following to account setups:
Setup 1:
$ cat .profile
...
export ENV=$HOME/.kshrc
alias ll="ls -l"
...
$ cat .kshrc
. $HOME/.profile
Setup 2:
$ cat .profile
...
export ENV=$HOME/.kshrc
...
$ cat .kshrc
alias ll="ls -l"
Which setup is advisable and why?
(added October 6, 2005)
More C programming
- Write a C program
which reads two integers from
stdin, a base and an exponent (in that order), computes the value
of the base raised to the exponent, and prints the resulting
product to stdout.
See the stdio(3), stdin(3),
stdout(3) scanf(3), and printf(3) manpages for help.
- Write a C program
which accepts two integers as command line arguments, a base
and an exponent (in that order), computes the value
of the base raised to the exponent, and prints the resulting product to stdout.
- Write a C program which
accepts three files as command line arguments.
The first file given at the command line contains only the base while the
second contains only the exponent. The program computes the value of the base
raised to the exponent, and prints the resulting product to the file given by
the third-command line argument.
- Write a C program with the same
requirements as the previous exercise
but this one
invokes the pow function in the math library to perform the
computation. See the pow(3) manpage for help.
Regular expressions and filters
- To match the strings "abc" and "abbbc" but not "ac", use the
extended regular expression:
- /ab*c/
- /ab+c/
- /ab?c/
- To match social security numbers, use the
regular expression:
- /[0-9]*/
- /[0-9]+/
- /[0-9]\{9\}/
- (true or false) The shell metacharacter . and the grep
metacharacter .
have different semantics?
- (true or false) vi and emacs are qualitatively different in that
vi has modes and emacs is modeless.
- (courtesy exercise 3-3, pp. 79 of [KP])
Create a file called grepfile, consisting of the
ten lines of exercise 3-3 in K&P (p. 79), one grep command per line.
Alternatively, you may copy grepfile from
~sperugin/text/grepfile.
grepfile is available from ...
grep \$
grep \\$
grep \\\$
grep '\$'
grep '\'$'
grep \\
grep \\\\
grep "\$"
grep '"$'
grep "$"
Give and explain the output of the following Korn commands:
grep -n "\$" grepfile
grep -n '\$' grepfile
grep -n \\\\ grepfile
grep -n '$' grepfile
echo 'Go $HOME'
echo "$5.00 is too much!"
echo $(who | wc -l) users is not very many
Programming shell filter scripts
For the follow three questions
do not use tools such as awk, Perl, Python, or
any other scripting language.
- Write a complete Korn shell script
which prints
to stdout all lines of its single file argument which contain more than
one word (any string of characters except whitespace).
- Write a complete Korn shell script
which prints
to stdout the lines of its single file argument which consist totally
of 5-letter (upper and lower case) palindromes. A palindrome is a word
which reads the same backwards and forwards (e.g., CbXbC)
- Write a complete Korn shell script
which prints
to stdout the list of files in the current directory (one per line),
together with their date of late modification (use "date filename" format).
- Write a complete Korn shell script with the
exact the same output as the preceding script, but this one invokes
awk.
- Suppose the output of ls -l
appears as follows (from p. 13 of [KP]), with
spaces being the field separators.
$ ls -l
total 2
-rw-r--r-- 1 you 19 Sep 26 16:25 junk
-rw-r--r-- 1 you 22 Sep 26 16:26 temp
Write a complete
Korn shell script which prints to stdout the list of files in the
current directory (one per line), together with their date of last
modification (use `month dd filename' format).
- without awk
- with awk
- The following is the bundle script from p. 98 of [KP]
$ cat bundle
# bundle: group files into distribution package
echo '# To unbundle, sh this file'
for i
do
echo "echo $i 1>&2"
echo "cat >$i <<'End of $i'"
cat $i
echo "End of $i"
done
$
Suppose we have the following two files, ab and xyz:
$ cat ab
hello
good
bye
$ cat xyz
Abc
Xyz
- What would be the stdout of the command line: $ bundle ab xyz
- In the bundle program, why is the first occurrence of
End of $i
single-quoted?
- Suppose we have a file ~/alongfile
containing many misspelt words, including
duplicates. Write a command line that would print to stdout
a count of the misspelt words excluding duplicates.
- What would the following command lines do:
- $ grep '\^[^x]' y
- $ sed 's/\*f/g/' h
- Write a command line which would print to stdout only
- lines 50-100 of a file mnop.
- all lines in a file mnop
which have only five characters in them and read the same
backwards as forwards (i.e., five-character palindromes).
- all the lines in the files f1 and f2 which
end with the literal string $HOME.
- Write a command line which tests if f1 is a directory.
- Consider the following ksh statements
(assume that they are executed in the order that they are given and that
the current directory is /home/lucy):
10 $ foo=null
11 $ print $foo
12 $ foo="$foo set"
13 $ print $foo
14 $ set -A x $foo
15 $ print ${x[1]}
16 $ unset foo
17 $ print ${foo:-unset}
18 $ integer pwd=3
19 $ if [[ ${pwd} = $(pwd) ]] ; then
20 > print 3
21 > else
22 > print $(( pwd*2 )) ; fi
23 $ A=quoted
24 print "A '$(print $A)' \$ and escaped \."
- What is printed by statement 13?
- What is printed by statement 15?
- What is printed by statement 17?
- What do statements 18-22 output (a syntax error is a valid answer)?
- What is printed by statement 24?
- Consider the following Korn shell script printargs.
#!/bin/ksh
for arg in "$@"; do
print $arg
done
What do each of the following command lines print?
- $ printargs a "b c" d
- $ printargs 'a "b c" d'
- Suppose we have a directory with 100 .c (C source) files.
Write a complete Korn shell
script which when invoked replaces the .c extension, on every
file in the current directory which contain it, with .cpp.
- Consider the following Korn shell script
(ls -t
prints to stdout the names of the files in the current directory sorted
by modification time):
#!/bin/ksh
ls -t | sed '/^'$1'$/q'
return 0
Explain what this does.
- Consider the following:
$ cat ids
111-22-4555
254-34-2341
314-34-4311
314-57-0001
701-09-1008
...
ids
is in the current directory and contains only valid social security
numbers, one per line, with no leading or trailing whitespace.
Write a command line to convert each line in the form xxx-yy-zzz
to xxxyyzzz and print the results numerically sorted to stdout.
- Consider the following:
$ cat names
Brown, Michael
Lowgator, Linus
Perry, Lucy
Caroll, Lewis
Austin, Jane
...
names
is in the current directory and
contains only one name (last, first)
per line, with no leading or trailing whitespace.
- Write a command line, which does not invoke awk,
to convert each line in the form (last,
first) to (first last)
and print the results alphabetically sorted to stdout.
- Write a command line, this time invoking awk,
to convert each line in the form (last,
first) to (first last)
and print the results to stdout.
Make and RCS
- What does the acronym RCS expand to?
- (true or false) RCS is a collection of Unix tools/commands
for software project management.
- (true or false) In a Makefile line, leading tabs and spaces are
insignificant.
- Consider the following:
$ ls -l
total 89
-rw------- 1 lucy users 196 Jun 25 09:41 Makefile
-rw------- 1 lucy users 90001 Jun 25 09:42 fig1.eps
-rw------- 1 lucy users 8 Jun 25 09:43 final.aux
-rw------- 1 lucy users 11056 Jun 25 09:43 final.dvi
-rw------- 1 lucy users 3664 Jun 25 09:43 final.log
-rw------- 1 lucy users 64411 Jun 25 09:44 final.ps
-rw------- 1 lucy users 8319 Jun 25 09:42 final.tex
$ cat Makefile
src = final.tex
all: final
final: final.ps
final.ps: final.dvi
dvips -o final.ps final
final.dvi: ${src} fig1.eps
latex final
clean:
touch *.tex
rm final.log final.aux final.dvi final.ps
$
Which commands, if any, do the following commands force to execute?
Consider the following two commands independently, i.e., the second is
not run after the first.
- $ make final
- $ make
More File manipulation, and Korn Shell
- Write a command line
to make (only) each plain file (not directories or links) ending
in .txt in or below your login directory
readable by you and writable by you and others,
without giving any extraneous permissions.
- Write a command-line to remove (only)
all plain files (not directories
or links) ending in .core residing in or below
your login directory.
- Write a command-line
to remove (only) all plain files ending in .core (only)
residing in your current working directory.
- Write a command-line to make (only) each directory (not plain files or
links) named abc or acc
residing in or below your current working directory readable by you and
your group; writable by you; and searchable by you, your group, and
everyone, without giving any extraneous permissions.
- Give one advantage and one disadvantage of ar.
- Give one advantage and one disadvantage of tar.
- Write a command line to tar and gzip (only) all the files (plain files,
directories, or links) ending in .c
in or below ~/C into C.tgz in one stroke.
-
Take the shell facilities described in the first chapter of K&P.
- How would you setup your environment such that files are
created readable by you, your group, and others; and writable
by you and your group, without giving any extraneous permissions,
in such a way that this change would be
in effect each time you logged in?
- Suppose you have a file $HOME/a/bfile.
How would you arrange it,
without giving any extraneous permissions, so that bfile would be
readable by you and your group, writable by you, and executable by
others?
- Give one example of file information which is in its parent
directory.
- What is the minimum number of links to the directory
d in the following figure, if circles represent directories and rectangles
non-directory files? Explain.
- Explain the following transcript. Assume
a plain file des exists in
the current working directory.
$ cat des
process patterns building
large scale systems
using object technology
$ ln des ~/b
$ rm des
$ cat ~/b
- (courtesy exercise 1-7, pp. 32-33 of [KP])
Explain the difference between
$ who | sort
and
$ who > sort
- (courtesy exercise 2-1, pp. 45 of [KP])
What happens when you type <ctrl-d> to ed?
Compare this to the following command:
$ ed < file
- (courtesy exercise 2-4, pp. 52 of [KP])
du was written to monitor disk usage. Using it to find
files in a directory hierarchy is at best a strange idiom, and perhaps
inappropriate.
As an alternative, look at the manual page for the find
command, and compare the two commands.
In particular, compare the command du -a | grep ... with the
corresponding invocation of find.
Which runs faster and how do you know? Is it better to build a new
tool or use a side effect of an existing tool?
- (courtesy exercise 2-6, pp. 62 of [KP])
Why does ls -l report 4 links ot recipes?
Hint: try
$ ls -ld /usr/you
Why is this useful information?
- (courtesy exercise 2-7, pp. 62 of [KP])
What is the difference between
$ mv junk junk1
and
$ cp junk junk1
$ rm junk
Hint: make a link to junk, then try it.
- Choose any file in /dev on our system. The fourth section
of the Unix Reference Manual on our system has descriptions of special
files. Use it to give a brief description (paraphrase the manual) of the
file you've selected. You may need to abbreviate the name of your file
when you invoke man.
If the file you've selected is a
"symbolic link," which it probably is, follow it to an "original."
Give the result of the ls -l command on that file, and explain
the fields. Does its access list begin with a -, d, or
l? Explain.
Click here for an example file
(do not use any contents from this in your solution). Your solution must take
the form of this sample and provide a commensurate level of detail.
- (courtesy exercise 3-14, pp. 94 of [KP])
Compare the here-document version of 411 with the original.
Which is easier to maintain? What is a better basis for a general
service.
|