How to logically negate the return value of a command?

Chris Green cl at isbd.net
Wed Sep 24 16:15:05 UTC 2014


On Wed, Sep 24, 2014 at 05:00:38PM +0100, Colin Watson wrote:
> On Wed, Sep 24, 2014 at 04:52:38PM +0100, Colin Watson wrote:
> > On Mon, Sep 22, 2014 at 07:04:57PM +0100, Chris Green wrote:
> > > I want to use ping to detect if a machine is available locally on a
> > > LAN as a test for a 'match exec' in ~/.ssh/config.
> > > 
> > > The trouble is that I need to logically negate the return value, i.e.
> > > I want to have something like:-
> > > 
> > >     match exec "not ping -c 1 hostname"
> > > 
> > > However this doesn't work of course.  Is there any easy way (i.e. an
> > > executable command) which will logically not the value that ping
> > > returns?  I know I could write a little script that does the job but
> > > if can do it without I'd be happier.
> > 
> > Match exec "! ping -c 1 hostname"
> 
> My bad, I should have tested; you are of course quite right that ssh
> prepends "exec" to the given command:
> 
>         /*
>          * Use "exec" to avoid "sh -c" processes on some platforms
>          * (e.g. Solaris)
>          */
>         xasprintf(&command_string, "exec %s", cmd);
> 
> How about, then:
> 
> Match exec "sh -c '! ping -c 1 hostname'"
> 
> A little cumbersome, but it works, and you can put more complicated
> things inside the double quotes.  The downside is that you can't use
> escaped double quotes in there as far as I know, so if it gets too
> complicated then the quoting will get rather fearsome, and you will
> probably eventually have to crack and write an auxiliary script.
> 
You've cracked it!  :-)

Thanks.  You're right about the escaped double quotes, I'd tried that
and it produces very odd errors.

-- 
Chris Green




More information about the ubuntu-users mailing list