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

5578 posts

Uber Geek

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.


Create new topic
BlueOwl
85 posts

Master Geek

Lifetime subscriber

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

How about

 

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

 

 


 
 
 

Shop now on AliExpress (affiliate link).
qwerty123
147 posts

Master Geek


  #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

5578 posts

Uber Geek

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.


Create new topic





News and reviews »

Bolt Launches in New Zealand
Posted 11-Jun-2025 00:00


Suunto Run Review
Posted 10-Jun-2025 10:44


Freeview Satellite TV Brings HD Viewing to More New Zealanders
Posted 5-Jun-2025 11:50


HP OmniBook Ultra Flip 14-inch Review
Posted 3-Jun-2025 14:40


Flip Phones Are Back as HMD Reimagines an Iconic Style
Posted 30-May-2025 17:06


Hundreds of School Students Receive Laptops Through Spark Partnership With Quadrent's Green Lease
Posted 30-May-2025 16:57


AI Report Reveals Trust Is Key to Unlocking Its Potential in Aotearoa
Posted 30-May-2025 16:55


Galaxy Tab S10 FE Series Brings Intelligent Experiences to the Forefront with Premium, Versatile Design
Posted 30-May-2025 16:14


New OPPO Watch X2 Launches in New Zealand
Posted 29-May-2025 16:08


Synology Premiers a New Lineup of Advanced Data Management Solutions
Posted 29-May-2025 16:04


Dyson Launches Its Slimmest Vaccum Cleaner PencilVac
Posted 29-May-2025 15:50


OPPO Reno13 Pro 5G Review 
Posted 29-May-2025 15:33


Logitech Introduces New G522 Gaming Headset
Posted 21-May-2025 19:01


LG Announces New Ultragear OLED Range for 2025
Posted 20-May-2025 16:35


Sandisk Raises the Bar With WD_BLACK SN8100 NVME SSD
Posted 20-May-2025 16:29









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.