Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Background: Here\'s a small shell script that compares two files: #!/bin/sh # co

ID: 3888900 • Letter: B

Question

Background: Here's a small shell script that compares two files:

#!/bin/sh

# compare f1 f2

# script to compare and comment on differences between 2 files

echo comparing $1 with $2

if diff $1 $2; then

echo true

else

echo false

fi

If you store this script in a file called compare and make that file executable with chmod +x compare, you can run it with ./compare f1 f2 for any 2 files f1 and f2. (Try it with small files.) Note, in the script, $1 is a reference to the first command line argument, and $2 is a reference to the second. This is really bad language design, but it works, so we live with it.

a) Does the diff command return an idication of success or failure? If so, when does it indicate success and when does it indicate failure. The above script lets you test this empirically.

b) In the notes for Lecture 8, the test file was threatening to get very long by the end. Suggest a script that could be used in the test script in order to allow each test to be coded on one line, assuming that there were existing files holding the input, the expected output, and the expected error messages for each test, stored in the testfiles directory.

Explanation / Answer

a)

The diff command has 3 types of exit codes which can be used for testing the success or the failure of the command. The description of the exit codes goes as follows :