1

I have to compare a file with 3 different golden files using diff. I need to exit the script with exit 0 if test file is the same as any of the three golden files.

I tried the following:

#!/bin/sh
one=`diff -q NEW_GOLDEN_OUTPUT_ASYNC_1 /tmp/tmp_last_lines.log`
two=`diff -q NEW_GOLDEN_OUTPUT_ASYNC_2 /tmp/tmp_last_lines.log`
three=`diff -q NEW_GOLDEN_OUTPUT_ASYNC_3 /tmp/tmp_last_lines.log`

if [[ $one || $two || $three ]]; then
  exit 0
else
  exit 1
fi

But it returns exit 0 in all cases. I'm using /bin/ksh shell. Any suggestions?

1
  • To check if two files are identical use if cmp -s "$source_file" "$dest_file"; then : # files are the same else : # files are different fi Commented Dec 7, 2012 at 10:13

1 Answer 1

1

Your code looks at the output of diff but you should look at the exit code. Try this instead:

#!/bin/sh
diff -q NEW_GOLDEN_OUTPUT_ASYNC_1 /tmp/tmp_last_lines.log && \
diff -q NEW_GOLDEN_OUTPUT_ASYNC_2 /tmp/tmp_last_lines.log && \
diff -q NEW_GOLDEN_OUTPUT_ASYNC_3 /tmp/tmp_last_lines.log

&& will only execute the next command if the previous one succeeded.

Alternatively, use set -e (Exit immediately if a command exits with a non-zero status.):

#!/bin/sh
set -e
diff -q NEW_GOLDEN_OUTPUT_ASYNC_1 /tmp/tmp_last_lines.log
diff -q NEW_GOLDEN_OUTPUT_ASYNC_2 /tmp/tmp_last_lines.log
diff -q NEW_GOLDEN_OUTPUT_ASYNC_3 /tmp/tmp_last_lines.log
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.