Shellscript action if two files are different Shellscript action if two files are different shell shell

Shellscript action if two files are different


if ! cmp test.py test.py~ >/dev/null 2>&1then  # restart servicefi

Breaking that down:

  • cmp test.py test.py~ returns true (0) if test.py and test.py~ are identical, else false (1). You can see this in man cmp.
  • ! inverts that result, so the if statement translates to "if test.py and test.py~ are different".
  • The redirects >/dev/null 2>&1 send all output of cmp to null device, so you just get the true/false comparison result, without any unwanted noise on the console.


You could diff() the two files. A return code of zero (0) means there are no differences. A return code of one (1) says the files differ.


I'd do something like

zumodo@shold$ cat /test/test/test/server.py | ssh zumodo@otherhost 'cat - > /test/test/test/test.py.new ; cmp /test/test/test/test.py /test/test/test/test.py.new || (mv /test/test/test/test.py.new /test/test/test/test.py ; echo issue restart command here)'