Bizarre failure of shell script 'diff'

The answer lies in how diff works. When diff finds a difference between the two files, its exit code is 1. Nonzero exit codes typically indicate an error, so when Keyboard Maestro sees that exit code, it bails out. If you look through the Engine.log file, you'll see something like

Task failed with status 1

which is true but not very helpful if you don't know diff's exit codes.

The workaround is to click on the gear icon in the upper left corner of your Execute Shell Script action and change the settings on "Failure Aborts Macro" and "Notify on Failure" from ✓ to ×. That should get you the output you expect.

20220120-212443

I assume diff returns a nonzero exit code when there are differences between the files so it can be used as a test for file equality. But it is surprising, given that most people use it to find differences they know are there.

By the way, you can avoid temporary files by using process substitution which is available in zsh and later versions of bash (e.g., the bash you can get from Homebrew). A shell script like

#!/bin/zsh
diff <(echo "$KMVAR_InstanceVar1") <(echo "$KMVAR_InstanceVar2")

will diff the contents of the two KM variables with no need to create and clean up temp files.

4 Likes