help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Fancy conditional expression: Have I got this right?


From: Stan Marsh
Subject: Fancy conditional expression: Have I got this right?
Date: Wed, 16 Jul 2025 13:28:54 -0600

I have the following line in a script.  I think it is right.  Can you confirm?

    ! echo hello | grep ll || [[ zoology =~ oolz ]] && echo "Fail!"

The idea is you have a pipeline of the form: cmd | grep ...
and you have a string comparison (using [[ ]] and =~).

You want to fail if either of the following is true:

    1) The pipeline (i.e., the "grep") fails
or:
    2) The string compare succeeds

The example above does *not* print "Fail!" (*) and this is because the grep 
succeeds and
the string comparison fails (because of the "z" in "oolz").

But you have to admit there are a lot of potential grey areas of precedence and
associativity to wonder about.

(*) It prints "hello" (from the "grep")

P.S. Not really interested in re-writes or alternative formulations.  Just want 
to be
sure the above is correct.  I.e., small edits are welcome.

=================================================================================
Please do not send me replies to my posts on the list.
I always read the replies via the web archive, so CC'ing to me is unnecessary.

Note that they always end up in my Spam file anyway, so it is annoying to have 
to
periodically clean that out.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]