Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I'm using a diff operation to check a couple of files, but am wondering if I can simultaneiously use it to trigger an event (e.g. echo -ing a line to terminal etc) without having to 'manually' check for the existence of the output report.

I had thought about using the report creation as a trigger, but as far as I know the file would be created regardless, it would just be empty in the event of no differences?

Basically, is it possible to have diff output a file, whilst inside an if statement, to keep my bash profile and the script itself as tidy as possible?

(If it helps, this is a follow on question to the wonderful help I received in this question: Notifications on next ssh login )

e.g. something to this effect?:

if [ diff filex filey > report.txt == true ]
   # So the report.txt is created but the 'state' of
   # the diff query is preserved and evaluated...
     then echo "Files are different"
         break

Hope that makes sense.

This is in Ubuntu bash FYI.

I did find that link, but it didn't appear to be considering the creation of a file from the output of diff whilst also using it to evaluate the statement, they were only doing the latter (but please do correct me if I've got that wrong?) – Joe Healey Jan 13, 2016 at 17:22 It's close enough that I thought it worth flagging. However, I also answered it. At least one of the answers there is the same as my answer below (although I point out that redirection makes no difference to the exit status and that's all the if statement uses, in this case). – Vatine Jan 13, 2016 at 17:26

Yes, diff exits with a status of 0 if there is no diffs (that's shell for "true"), so you can do it like this:

if ! diff <file1> <file2>
  do the thing when there are diffs
  do the thing when all is same

This should work even if you redirect the output.