Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


Lias

5657 posts

Uber Geek
+1 received by user: 3978

ID Verified
Trusted
Lifetime subscriber

#260189 15-Nov-2019 09:23
Send private message

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?

 

 

 

 

 

 





I'm a geek, a gamer, a dad, a Quic user, and an IT Professional. I have a full rack home lab, size 15 feet, an epic beard and Asperger's. I'm a bit of a Cypherpunk, who believes information wants to be free and the Net interprets censorship as damage and routes around it. If you use my Quic signup you can also use the code R570394EKGIZ8 for free setup. Opinions are my own and not the views of my employer.


Create new topic
BlueOwl
87 posts

Master Geek
+1 received by user: 69

Lifetime subscriber

  #2353744 15-Nov-2019 09:38
Send private message

How about

 

if grep hello test.txt ; then false ; else true ; fi

 

 




qwerty123
147 posts

Master Geek
+1 received by user: 20


  #2353760 15-Nov-2019 09:45
Send private message

Just use negate operator before the command.

 

~$ grep blah examples.desktop; echo $?
1

 

~$ ! grep blah examples.desktop; echo $?
0

 

 


Lias

5657 posts

Uber Geek
+1 received by user: 3978

ID Verified
Trusted
Lifetime subscriber

  #2353765 15-Nov-2019 09:56
Send private message

Damn that was fast.. thanks folks :-)





I'm a geek, a gamer, a dad, a Quic user, and an IT Professional. I have a full rack home lab, size 15 feet, an epic beard and Asperger's. I'm a bit of a Cypherpunk, who believes information wants to be free and the Net interprets censorship as damage and routes around it. If you use my Quic signup you can also use the code R570394EKGIZ8 for free setup. Opinions are my own and not the views of my employer.


Create new topic








Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.