How to logically negate the return value of a command?

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


On Wed, Sep 24, 2014 at 10:27:42AM -0400, Nathan Dorfman wrote:
> 
> On Tue, Sep 23, 2014 at 2:26 PM, Chris Green <cl at isbd.net> wrote:
> > On Tue, Sep 23, 2014 at 11:34:41AM -0400, Nathan Dorfman wrote:
> >> On Mon, Sep 22, 2014 at 2:04 PM, Chris Green <cl at isbd.net> wrote:
> >> > 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.
> >>
> >>
> >> There's probably a nicer way to do it, but this should work:
> >>
> >>     ping -c1 hostname; test "$?" -ne 0
> >>
> > That won't work in this case because there's no shell running when the
> > program is exec'ed from the ssh config file.
> >
> Are you sure about that? I haven't tried it, but the man page
> explicitly states that it will be run from the shell:
> 
>     The exec keyword executes the specified command under the user's shell.
>     -- http://man7.org/linux/man-pages/man5/ssh_config.5.html
> 
> If it doesn't work, you could always make it exec sh -c 'ping -c1
> hostname; test "$?" -ne 0'. I would also suggest notifying the ssh
> team that their man page is in error if this is the case.
> 
You're right, it does say "under the user's shell" but I've tried lots
of different things and as soon as there is any shell syntax in the
thing that's exec'ed it seems to fail.

I'm playing some more, one major problem is that the string has to be
quoted (as it has spaces in it) and it seems that the exec only
accepts ", if you try ' it gives very odd errors.

The following produces no errors, but doesn't work (as in the test
returns true when it shouldn't):-

    Match host chris exec "ping -c1 chris;test $? -ne 0"
        ProxyCommand ssh cheddar nc -q0 zbmc.eu 22 

As I said I can't use single quotes, it just falls in a heap. If I do:-

    Match host chris exec "ping -c1 chris;test \"$?\" -ne 0"
        ProxyCommand ssh cheddar nc -q0 zbmc.eu 22

Then I get:-

    chris at x201$ ssh chris
    Missing Match criteria for $?\\ -ne 0
    /home/chris/.ssh/config line 16: Bad Match condition

I think the exec's handling of commands passed to the user's shell is
somewhat odd! :-)


-- 
Chris Green




More information about the ubuntu-users mailing list