3

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.

6
  • 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, 2012 at 20:02
  • What do you mean by "negate"? Exit codes aren't boolean. Oct 19, 2012 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, 2012 at 20:07
  • 1
    @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, 2012 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, 2012 at 20:14

2 Answers 2

3

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

system("sh", "-c",  "! \"$0\" \"$@\"", "cmd", "arg1", "arg2"...)

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.

-3

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>`
3
  • 3
    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, 2012 at 19:59
  • 1
    You're right, the command in principle is wrong, but it may work in its specific case!
    – ilbazzo
    Oct 19, 2012 at 20:06
  • i would also argue that <yourFirtsCommand> its a shell feature
    – aleivag
    Jul 15, 2020 at 17:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .