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.




3947 posts

Uber Geek

Trusted
Lifetime subscriber

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

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?

 

 

 

 

 

 





Information wants to be free. The Net interprets censorship as damage and routes around it.


Create new topic
28 posts

Geek


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

How about

 

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

 

 


120 posts

Master Geek


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

Just use negate operator before the command.

 

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

 

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

 

 


 
 
 
 




3947 posts

Uber Geek

Trusted
Lifetime subscriber

  # 2353765 15-Nov-2019 09:56
One person supports this post
Send private message quote this post

Damn that was fast.. thanks folks :-)





Information wants to be free. The Net interprets censorship as damage and routes around it.


Create new topic



Twitter and LinkedIn »



Follow us to receive Twitter updates when new discussions are posted in our forums:



Follow us to receive Twitter updates when news items and blogs are posted in our frontpage:



Follow us to receive Twitter updates when tech item prices are listed in our price comparison site:





News »

Ring launches indoor-only security camera
Posted 23-Jan-2020 17:26


New report findings will help schools implement the digital technologies curriculum content
Posted 23-Jan-2020 17:25


N4L to upgrade & support wireless internet inside schools
Posted 23-Jan-2020 17:22


Netflix releases 21 Studio Ghibli works
Posted 22-Jan-2020 11:42


Vodafone integrates eSIM into device and wearable roadmap
Posted 17-Jan-2020 09:45


Do you need this camera app? Group investigates privacy implications
Posted 16-Jan-2020 03:30


JBL launches headphones range designed for gaming
Posted 13-Jan-2020 09:59


Withings introduces ScanWatch wearable combining ECG and sleep apnea detection
Posted 9-Jan-2020 18:34


NZ Police releases public app
Posted 8-Jan-2020 11:43


Suunto 7 combine sports and smart features on new smartwatch generation
Posted 7-Jan-2020 16:06


Intel brings innovation with technology spanning the cloud, network, edge and PC
Posted 7-Jan-2020 15:54


AMD announces high performance desktop and ultrathin laptop processors
Posted 7-Jan-2020 15:42


AMD unveils four new desktop and mobile GPUs including AMD Radeon RX 5600
Posted 7-Jan-2020 15:32


Consolidation in video streaming market with Spark selling Lightbox to Sky
Posted 19-Dec-2019 09:09


Intel introduces cryogenic control chip to enable quantum computers
Posted 10-Dec-2019 21:32



Geekzone Live »

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


Support Geekzone »

Our community of supporters help make Geekzone possible. Click the button below to join them.

Support Geezone on PressPatron



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.

Alternatively, you can receive a daily email with Geekzone updates.