Hi GZ,
I'm banging my head against a wall trying to achieve this, so I'm crowd sourcing some help :-)
I need way to invert the exit code of a command, so that the $? exit code variable contains 0 for a failure and 1 or higher for success.
Using grep as an example (but the solution should work with anything, not just grep):
if /tmp/test.txt contains 10 lines, one of which is hello, grepping the file for hello will return 0, and return 1 if it didn't contain it.
ubuntu:/tmp$ grep hello test.txt;echo $?
hello
0
ubuntu:/tmp$ grep hesllo test.txt;echo $?
1
ubuntu:/tmp$
I need to invert that, and the solution needs to work with a single command line rather than a shell script.
I've tried things like grep hello test.txt && false || true, using (exit 1) instead of true/false, using | [ $? -eq 0 ] etc without much luck.
I need this for AWS cfn-init command tests. From the documentation,
A test command that determines whether cfn-init runs commands that are specified in the command key. If the test passes, cfn-init runs the commands. The cfn-init script runs the test in a command interpreter, such as Bash or cmd.exe. Whether a test passes depends on the exit code that the interpreter returns.
For Linux, the test command must return an exit code of 0 for the test to pass
E.g. if I want an init command that adds an entry to a file, I need the test to return 0 if the file doesn't have that entry, and 1+ if it does. If I don't have a test that works like this, it won't be idempotent, which is pretty vital.
Help?