Combining and negating test expressions in Bourne sh? - Unix

This is a discussion on Combining and negating test expressions in Bourne sh? - Unix ; Hello folks, does somebody know whether it is possible to negate several test expressions in Bourne shell with a single NOT? E.g. I have the two expressions [ $a -gt 0] and [ $str = "" ] I can do ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Combining and negating test expressions in Bourne sh?

  1. Combining and negating test expressions in Bourne sh?

    Hello folks,

    does somebody know whether it is possible to negate several test
    expressions in Bourne shell with a single NOT?

    E.g. I have the two expressions

    [ $a -gt 0] and [ $str = "" ]

    I can do

    if [ $a -gt 0] && [ $str = "" ]
    then
    ....

    in Bourne shell.

    but is it possible in a simple way to test the result of the negation
    of both expressions

    in pseudocode

    if not ( [ $a -gt 0] && [ $str = "" ] )
    then
    ....

    instead of

    if [ ! $a -gt 0] && [ ! $str = "" ] )
    then
    ....

    which works in Bourne shell (the equivalent of the pseudocode above
    won't).

    Cheers


    Bernd


  2. Re: Combining and negating test expressions in Bourne sh?

    On 2006-10-27, bernd wrote:
    > Hello folks,
    >
    > does somebody know whether it is possible to negate several test
    > expressions in Bourne shell with a single NOT?
    >
    > E.g. I have the two expressions
    >
    > [ $a -gt 0] and [ $str = "" ]
    >
    > I can do
    >
    > if [ $a -gt 0] && [ $str = "" ]
    > then
    > ...
    >
    > in Bourne shell.
    >
    > but is it possible in a simple way to test the result of the negation
    > of both expressions
    >
    > in pseudocode
    >
    > if not ( [ $a -gt 0] && [ $str = "" ] )
    > then
    > ...
    >
    > instead of
    >
    > if [ ! $a -gt 0] && [ ! $str = "" ] )
    > then
    > ...
    >
    > which works in Bourne shell (the equivalent of the pseudocode above
    > won't).


    [ $a -gt 0] || [ $str = "" ] || {
    : do whatever
    }

    --
    Chris F.A. Johnson, author |
    Shell Scripting Recipes: | My code in this post, if any,
    A Problem-Solution Approach | is released under the
    2005, Apress | GNU General Public Licence

  3. Re: Combining and negating test expressions in Bourne sh?

    Hello, Chris,

    thanks for Your advice. Your solution is more professional and a little
    more compact than negating each single test-expression.

    BTW: Visited Your homepage. As a former web-programmer I found Your
    article "Broken by Design" interesting and amusing at the same time.
    Unfortunately technical writers here in Germany, whether in the net or
    those busy with writing books, rarely seem to have
    humour. I cannot understand why those guys seem to be of the opinion
    that technical or scientific topics and humour exclude each other.

    Next time I visit my book dealer I will check whether You continue with
    this entertainig style in Your book ;-)

    Cheers


    Bernd

    Chris F.A. Johnson schrieb:

    > On 2006-10-27, bernd wrote:
    > > Hello folks,
    > >
    > > does somebody know whether it is possible to negate several test
    > > expressions in Bourne shell with a single NOT?
    > >
    > > E.g. I have the two expressions
    > >
    > > [ $a -gt 0] and [ $str = "" ]
    > >
    > > I can do
    > >
    > > if [ $a -gt 0] && [ $str = "" ]
    > > then
    > > ...
    > >
    > > in Bourne shell.
    > >
    > > but is it possible in a simple way to test the result of the negation
    > > of both expressions
    > >
    > > in pseudocode
    > >
    > > if not ( [ $a -gt 0] && [ $str = "" ] )
    > > then
    > > ...
    > >
    > > instead of
    > >
    > > if [ ! $a -gt 0] && [ ! $str = "" ] )
    > > then
    > > ...
    > >
    > > which works in Bourne shell (the equivalent of the pseudocode above
    > > won't).

    >
    > [ $a -gt 0] || [ $str = "" ] || {
    > : do whatever
    > }
    >
    > --
    > Chris F.A. Johnson, author |
    > Shell Scripting Recipes: | My code in this post, if any,
    > A Problem-Solution Approach | is released under the
    > 2005, Apress | GNU General Public Licence



  4. Re: Combining and negating test expressions in Bourne sh?

    bernd wrote:
    > but is it possible in a simple way to test the result of the negation
    > of both expressions
    >
    > in pseudocode
    >
    > if not ( [ $a -gt 0] && [ $str = "" ] )
    > then


    One way to write "if not", if the body is just a simple command, is
    this:

    [ $a -gt 0 ] && [ $str = "" ] || echo "conditions not met!"

    >
    > instead of
    >
    > if [ ! $a -gt 0] && [ ! $str = "" ] )
    > then
    > ...


    That is an incorrect inversion. Google for "De Morgan's law".

    not (x1 and x2 and x3 ... xn) == (not x1) or (not x2) or ... (not xn)

    When the "not" operator is distributed into the terms, but the and's
    have to change to or's. So the correct inversion should be:

    if [ ! $a -gt 0 ] || [ ! $str = "" ]

    You can invert the tests differently for better readability:

    if [ $a -le 0 ] || [ $str != "" ] ..

    You can nest expressions within the [ ... ] test like this, but it's
    ugly:

    if [ ! \( $a -gt 0 \) -o \( $str != "" \) ] ...

    Isn't that idiotic? Why can't the [ command understand [ ] brackets,
    which don't require escaping?

    This stuff was desi^H^H^H^Hcoded up by morons.


+ Reply to Thread