I have an executable that I will test with all input files and make a corresponding number of output files (I can do this part). Then I will diff the newly made files, called mytestN.out, with the files with the correct outputs, called testN.out and pipe it into a file called difftestN.txt. Then I will check each of the difftest.txt and if it is empty, print "success" and if not empty, print "difference found".

For example, I will do

./Executable < test1.in > mytest1.out

for the N number of test.in's. I found that this can be done with this

for f in test*.in; do ./tetris <"$f" >"my${f%in}svg"; done

Then I will do diff command for all mytest.out's against all test.out's.

diff mytest1.out test1.out > difftest1.txt

I'm not sure how to do this for all testN's. I think this is the right approach:

for f in mytest*.svg; do diff "$f" "###what goes here?###" > "diff${f%in}txt"; done

but I don't know what goes in after "$f", and the diff will be piped into a diffmytestN.txt, but i want difftestN.txt

Then, I want to check each of the difftest.txt's and if it is empty, print "success" and if it is not empty, print "difference found"

  • 1
    for f in test*.out; do diff "my$f" "$f" ...? – muru Nov 17 '15 at 4:17

You can adjust this for an arbitrarily large number of files named as you mentioned. Here are 4 test files' contents I used: test1: one mytest1: 1 test2: two mytest2: two

for i in {1..2}; do 
    diff test${i}.out mytest${i}.out &>/dev/null
    if [ $es -ne 0 ]; then 
        echo "difference found"
        echo "success"


difference found

I realize I have not answered your literal question, because you asked for files holding the output of diff. If you really must have that, you could instead change the code:

  diff test${i}.out mytest${i}.out > difftest${i}.txt

  if ! [[ -s difftest${i} ]]; then 
      echo success
      echo "difference found"

It's probably useful to keep in mind that, should your program ever try to compare a file with content to a non-existent file, it'll create output to stderr that looks as if there was a substantive difference between the file and non-existent file. This should be fine since only stdout goes into *.txt.

  • Correction, this should have read, 'should your program even try to compare and empty file to a non-existent file, it'll wind up printing difference found.' – Erik Bryer Nov 19 '15 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.