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?

2
  • It's hard to say definitively since you haven't given an example, however I think it's probably your attempt to nest single quotes '. . . |\'[^\']*\'| . . .'. One option that may work is to close and reopen the outer quoting i.e. |'\''[^'\'']*'\''| Nov 30, 2017 at 13:18
  • steeldriver You should propose this as the answer as you are correct.
    – Peter A
    Nov 30, 2017 at 21:47

1 Answer 1

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"
!"#%&/()[]{}'-.,*

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .