Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there any standard command that will negate the the exit code of another command provided as an argument without using shell features, or starting a shell?

I have a program that does something based on exit code of an system() call. I have another program that returns its status via the exit code. But the program I am calling returns the results opposite from what I want. I can simply specify something like bash -c '....', but is there an easier way?

P.S. Yes, I know I am probably trying to optimize something that probably doesn't really matter all that much. But mostly I am curious, since unix seems to have a command for almost everything, why I can't find a simple negate-the-exit-code command.

share|improve this question
    
Like sch, I don't think there is an easier way. But use sh -c ... not bash -c ...; it may be much faster for you. –  dubiousjim Oct 19 '12 at 20:02
    
What do you mean by "negate"? Exit codes aren't boolean. –  jgoldschrafe Oct 19 '12 at 20:07
    
On these particular systems /bin/sh is just a symlink to bash. I could install a lightweight shell. The performance isn't that big of a deal really. –  Zoredache Oct 19 '12 at 20:07
    
@jgoldschrafe, lets assume that 0=true (like /bin/true), and anything else is false. So if the exit is not 0 then return 0, and if it is zero, then return something not zero. –  Zoredache Oct 19 '12 at 20:09
    
Are you talking about C? The exec family of functions don't actually return. Are you talking about system() instead? –  jw013 Oct 19 '12 at 20:14
show 1 more comment

2 Answers

Not in the standard tool set AFAIK. sh (and its !) in the standard tool set would be the closest to what you want.

exec("sh", "-c".  "! \"$0\" \"$@\"", "cmd", "args")

Other tools that can execute commands either don't have that feature (find, env, xargs...) or call a shell themselves (ed, awk...) to execute a command.

Early unices had an "if" command, but it disappeared with the Bourne shell that brought extended control statements.

share|improve this answer
add comment

Try this:

[ ! `<yourFirtsCommand>` ]

Replace <yourFirtsCommand> with your command. I recommend you use the right kind of quotes!
to avoid problems with [ ] you can use:

test ! `<yourFirtsCommand>`
share|improve this answer
1  
This is probably not what you want. What this will do is collect the string output of yourFirtsCommand, and negate it. Negating a empty string returns 0 (shell-speak for true), and negating a non-empty string returns 1. This will give you trouble if the output has spaces in it (in that case you need an extra layer of "..." quotes), and it will ignore the exit code of yourFirtsCommand. Plus a shell is needed to handle the angle quotes; I don't think you could just directly exec /bin\[ to get this result. The best solution will just use a shell and ! without [...]. –  dubiousjim Oct 19 '12 at 19:59
    
You're right, the command in principle is wrong, but it may work in its specific case! –  ilbazzo Oct 19 '12 at 20:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.