mainThe GNU Bourne-Again SHell - Support: sr #110463, operator '=~' condition is wrong

 
 

sr #110463: operator '=~' condition is wrong

Submitter:  Kory Chiu <korychiu>
Submitted:  Tue 23 Mar 2021 07:43:40 AM UTC
   
 
Category:  None Priority:  5 - Normal
Severity:  3 - Normal Status:  Invalid
Privacy:  Public Assigned to:  None
Open/Closed:  Open Operating System:  GNU/Linux
* Mandatory Fields

Add a New Comment Rich Markup
   

Tue 30 Mar 2021 02:39:48 AM UTC, comment #2: 

Thanks for the correction.

Kory Chiu <korychiu>
Thu 25 Mar 2021 07:06:03 PM UTC, comment #1: 

Your usage is wrong. AND-OR lists are not a straight substitute for if statements. They are left-associative and have equal precedence.

This means that your first example is equivalent to

{ [[ "$_config" =~ "vhodddst.conf" ]] || echo "false" ; } && echo "true"

Since the group command returns the status of the echo command, the command returns success and the command to the right of the && gets executed.

In the original example, then, the exit status of the OR list is the exit status of the echo, and, since that is successful, the rest of the AND list is executed.

You should be able to follow the execution of your second example to see why it differs.

Chet Ramey <chet>
Group administrator
Tue 23 Mar 2021 07:43:40 AM UTC, original submission:  

# _config="vhost.conf"

# [[ "$_config" =~ "vhodddst.conf" ]] || echo "false" && echo "true"
false
true

# [[ "$_config" =~ "vhost.conf" ]] || echo "false" && echo "true"
true


# [[ "$_config" =~ "vhodddst.conf" ]]
# echo $?
1




bash --version
GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.


Kory Chiu <korychiu>

 

(Note: upload size limit is set to 16384 kB, after insertion of the required escape characters.)

Attach Files:
   
   
Comment:
   

No files currently attached

 

Depends on the following items: None found

Items that depend on this one: None found

 

Carbon-Copy List
  • -email is unavailable- added by chet (Posted a comment)
  • -email is unavailable- added by korychiu (Submitted the item)
  •  

    There are 0 votes so far. Votes easily highlight which items people would like to see resolved in priority, independently of the priority of the item set by tracker managers.

    Only logged-in users can vote.

     

    Follows 1 latest change.

    Date Changed by Updated Field Previous Value => Replaced by
    2021-03-25 chet StatusNone Invalid

    Back to the top

    Powered by Savane 3.13-758e.
    Corresponding source code