Since nobody has answered the question
(“what is happening on this line of code?” –
“I’m struggling to find out what this line means”):
The script is testing the C program.
What it’s doing is basically equivalent to
# Run the program with input “1 2 5” and write its output to a file. Since the program
# is supposed to check the input for the minimum number, we expect it to output “1”.
echo "1 2 5" | $PROGRAM > file1
# Create a second file that contains the known correct output (minimum)
# for this input (i.e., “1”).
echo 1 > file2
# Compare the files. diff’s standard output and standard error will go to the stdout
# and stderr of the script, which is the terminal unless the user does I/O redirection.
# The `if` will test diff’s exit status.
if diff -u file1 file2
then
# Exit status 0 means the files are identical;
# i.e., the program’s output is correct; i.e., the test passes. Do nothing.
:
else
# Exit status non-zero (probably 1) means that the files are different;
# i.e., the program’s output is wrong; i.e., the test fails.
echo "test failed on 1 2 5"
((FAILURES++))
fi
rm file1 file2
Why was the script written that way?
A really good question.
Perhaps you can ask the author of the script and relay their answer to us.
There’s no need to use files.
And, OK, they aren’t using files;
they are, as Andy Dalton and noelbk have explained,
using process substitution —
which is pipes, and which is not POSIX-compliant.
And, yes, you need to use either a file or a pipe
to capture the output from the program.
But you don’t need either a file or a pipe to hold the correct output.
The script can be rewritten
if [ "$(echo "1 2 5" | $PROGRAM)" != 1 ]
then
echo "test failed on 1 2 5"
((FAILURES++))
fi
which uses a command substitution to capture the output from the program,
and then just puts the correct output on the if
command line.
- Why use the
-u
option of diff
?
- This option is documented as
“output NUM (default 3) lines of unified context”.
So this means that, if you have two files that are 100 lines long,
and they are identical except for line 42,
then
diff -u
will show you lines 39-45
(three above and three below the one that’s different).
But this is meaningless
when one of the inputs is known to be only one line long,
and the other is expected to be only one line long.
- An undocumented feature of the
-u
option of diff
is that it shows the modification times of the inputs.
But, in the given script, the inputs to diff
are process substitutions,
which are dynamically created pipes.
So the modification times of each are the current time —
in other words, on-screen clutter.
- You should always quote shell variables (e.g.,
"$PROGRAM"
)
unless you have a good reason not to.
Why does the script report that your program failed?
It’s impossible to tell without seeing your program,
or at least its output.
Is it possible that your program is including a space in its output,
or a carriage return (\r
) instead of or in addition to a newline?
Or maybe even an extra newline (i.e., a blank line)?
Do this
echo "1 2 5" | your_program | od -cab
(If
od
reports that it doesn’t recognize all the options,
leave out the one it complains about; e.g.,
-cb
or
-ab
.)
The output should, of course, be a
1
and a newline (
\n
).