0

I have a Magento 2 installation where I am attempting to find any syntactiaclly incorrect xml files.

I am trying to use the below command to find any acl.xml files in the code dir that contain a node labelled resource but missing an id attribute.

I have tested this regex code on an online regex tester successfully:

<(?:resource)(?:\s+(?!id\b)[\w\-.:]+(?:\s*=\s*(?:"[^"]*"|\'[^\']*\'|[\w\-.:]+))?)*\s*/?>

When I try to use the below command however, I get an error:

find app/code -type f -name "acl.xml" | xargs ack '<(?:resource)(?:\s+(?!id\b)[\w\-.:]+(?:\s*=\s*(?:"[^"]*"|\'[^\']*\'|[\w\-.:]+))?)*\s*/?>'

-bash: syntax error near unexpected token `)'

Any idea why this command is not working?

pa4080
  • 30,621
Peter A
  • 31

1 Answers1

0

The regexp monster there contains single quotes, which end the quoted string:

find ... '<(?:resource)...(?:"[^"]*"|\'[^\']*\'|[\w\-.:]+))?)*\s*/?>'
#        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^   ^^^^^  

Backslashes don't work to escape anything within single quotes, so the marked parts are quoted, the stuff outside of them isn't. With the special characters there, the shell gets confused. (Well, I'd get confused if I tried to parse that by hand.)

You could work around that at least by changing to double-quotes, so that backslashes could be used to escape the double-quotes within the string.

Another way would be to abuse command substitution and here-documents, that should be able to deal with quotes without further escaping:

$ string=$(cat <<'EOF'
!"#%&/()[]{}'-.,*
EOF
)
$ echo "$string"
!"#%&/()[]{}'-.,*
ilkkachu
  • 1,887