4. [10 marks]

Which of the quotation marks in this shell script are unnecessary?

foo &
foopid="$!"
while kill -0 "$foopid"
do
    message="`foostatus`"
    if test "$message" = "Terminated"
    then
        exit 0
    else
        echo foo status: $message
    fi
done
exit 0

A few notes:

For each set of quotation marks, either explain why it is necessary (for example by giving a situation in which they would prevent an error), or explain (also very tersely) why they are not necessary.

a) "$!"

Answer: Unnecessary. The process ID is a simple number, no funny characters.

b) "$foopid"

Answer: Again unnecessary. We know that this has the value of a previous access to "$!", so it's going to be just a number.

c) "`foostatus`"

Answer: Necessary. The status may contain spaces, wildcards, anything; and we want it all to go into the variable 'message' without further interpretation.

d) "$message"

Answer: Necessary. Without the quotes, the message will be re-parsed, and if it contains spaces (which the example suggests it usually will), it will occupy multiple arguments to test, and test will give a syntax error.

e) "Terminated"

Answer: Unnecessary. All of the characters in the string are letters. The quotation marks don't do anything because there are no special characters to suppress the interpretation of.


[press 'back' in your web browser to return to where you were in the exam]