ECS 40 Practice Midterm #1 Key
ECS 40 Practice Midterm #1 Key
1. Write a command line to display all the files in your hw1 directory and the all of the files in its subdirectories.
ls -aR hw1
2. Write a command line to display only the files in your hw1 directory, and not the files in its subdirectories.
ls hw1
Given the following session answer questions 3 to 5.
dec63% ps
PID TT STAT TIME COMMAND
1262 p0 Z 0:00
753 p0 S 0:04 -tcsh (tcsh)
1236 p0 T 0:00 tcsh d.sh
1241 p0 T 0:00 tcsh e.sh
1246 p0 T 0:00 tcsh d.sh
1251 p0 T 0:00 tcsh e.sh
1256 p0 T 0:00 tcsh d.sh
1261 p0 T 0:00 tcsh e.sh
1264 p0 R 0:00 ps
dec63%
3. (8 points) Explain what the Z, S, T, and R each mean in the STAT column.
R = runnings, S = short-term sleep (less than 20 seconds), T = suspended, Z = Zombie process (terminated but
still cleaning up after itself)
4. (9 points) Write a command line that would print out the information about the first two processes that have a status
of T, i.e., 1236 and 1241
ps | grep ” T ” | head -2
5. (8 points) Write a command line that would print out the number of processes that do not have status T, i.e., the output
would be 3.
ps | grep -v ” T” | wc -l
6. (5 points) Write a command line to archive into a file, named arch, all of my header files and C source code files in
my current directory.
tar -cf arch *.h *.c
7. (5 points) I’ve created a C source code file using vi with a umask of 425. What will be its permissions?
242 or -w- r– -w-
8. (5 points) Most system executables are stored in subdirectories named bin. Write a command line that lists all of the
bin directories on the computer.
find / -type d -name bin
9. (25 points) You have a C++ project that uses four files: 1) car.cpp; 2) car.h; 3) body.cpp; and 4) body.h; . Write a
make file that creates an executable named automobile based on the following information:
. car.cpp depends on body.h, and car.h.
body.cpp depends on body.h
automobile: car.o body.o
g++ -o automobile car.o body.o
car.o: car.cpp car.h body.h
g++ -c car.cpp
body.o: body.cpp body.h
g++ -c body.cpp
10. ( 35 points) Given the following script answer the questions..
1. #! /bin/bash
2. var=`echo $@ | awk ‘{print $NF}’ `
3. if [ -d $var ] ; then
4. for var2 in $@ ; do
5. if test $var2 != $var ; then
6. ln $var2 $var/$var2
7. rm $var2
8. fi
9. done # for
10. else
11. ln $1 $2
12. rm $1
13. fi
a) (3 points) What does line 1 do?
Tells the shell that /bin/bash should execute this shell script.
b) (8 points) What does line 2 do?
Assigns the value of the last command line parameter to the variable var.
c) (3 points) What does line 3 do?
Test if the last parameter is a directory.
d) (3 points) What does line 4 do?
Assigns the variable var2 the values of the command line parameters in order.
e) (3 points) What does line 5 do?
Tests if the current command line parameter is the same as the last command line parameter.
f) (3 points) What does line 6 do?
Creates a link to the file named in the variable var2 with a file with the same name in the directory given as the last
command line parameter.
g) (3 points) What does line 11 do?
Creates a link from the first parameter to the second parameter.
h) (9 points) What does the script, as a whole, script do?
This script implements the mv command. It looks at the last argument on the command line and determines
whether it is a regular file or a directory. If it is a directory, then the script goes through command line
parameters one by one and moves the files named from the current location to the directory named in the last
parameter. If the last parameter is a regular file, then the script renames the file named in the first parameter to
the file named in the second (and one hopes last) parameter.