Shell MCQs

Mar 12, 2015 • Matthew Collins

These questions are from the shell scripting lesson: Shell script lesson

Question 1

Given the script containing

echo "$4", "$3", "$2", "$1"

What is the output of

$ bash a b "c d"

a. a, b, c, d

b. d, c, b, a

c. c d, b, a

d. , c d, b, a

Question 2, Piping your own scripts

With this question I wanted to make sure everyone understands that there is no difference between existing programs and the scripts they write. Everything outputs to the same stdout so pipes can be used with both. I also wanted to get people to think about having not just one massive script; they too can write a bunch of small things to chain together.

What is the output of the following code?

bash *[AB].txt | bash
  1. A sorted list of file names processed by (They’ve mistaken the command sort for the script they wrote OR they forgot that sorted outputs the sorted number of lines and does not just sort names of files.)

  2. An error because expects a parameter with a “*” in it. (They have think that “$@” is a placeholder only for file globs like the example.)

  3. An error because the output of one script can’t be sent to another script. (The answer I am really checking to make sure they do not give.)

  4. A sorted list of the total number of lines in each file that was processed by (The right answer.)