I have a bash script that expands two archives (jar files) into two directories and then does a diff of the two folders. I want the diff to be piped into a file so I do this in the bash file.

diff -r $originalJarExtracted $assembledJarExtracted >target/$diffList

While this works it also stops the script and anything afterwards is not executed. How can I get diff to pipe its output into the file and for the script to keep going. I looked at the info and man pages for diff but could not get this to work. I am running the script on the mac btw.

Just for reference the scripts are on github

  • The script will keep executing. Post the whole script (or a reduced script that exhibits the problem, if the whole script is very long). – Gilles 'SO- stop being evil' Jan 5 '12 at 0:00
  • Have you tried putting it in background &? A nice trick when debugging your scripts is to execute it like bash -x yourscript It will output the execution trace with the contents of variables to standard output/error. – user13742 Jan 5 '12 at 0:05

Wild guess: your script is operating under set -e (synonym: set -o errexit), which causes the shell to exit if any commands returns a nonzero status (conventionally indicating failure). If so, it will exit if diff finds any difference, because diff returns the status 1 if it finds differences. If this is the problem, then change that line to

diff -r "$originalJarExtracted" "$assembledJarExtracted" >"target/$diffList" ||
[ $? -eq 1 ]

Then the script will only exit if diff returns a status other than 0 (no difference) or 1 (differences), i.e. only if there was an error such as a missing file.

  • Your wild guess was correct. If I take set -e out the script works. Adding your suggested changes however does not fix it, but at least I can run the scripts all in one now.. – Manfred Moser Jan 5 '12 at 0:25

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.