问题
I'm translating a GNU Makefile into MSBuild XML.
I have the following sed command (part of a larger command):
... | sed 's/^Q(.*)/"&"/' | ...
When I execute just that sed portion in Cygwin, it "works" in the sense that it doesn't error out.
However, after I've translated it to MSBuild XML - replaced the XML-sensitive symbols with ", &, ' - I get the error
sed: unsupported command '
I'm sure it's not an issue with XML escaping issues; the Visual Studio build log says
Task Parameter:Command="C:\Program Files\GNU ARM Eclipse\Build Tools\2.8-201611221915\bin\sed" 's/^Q(.*)/"&"/' (TaskId:21)
sed: unsupported command ' (TaskId:21)
The command ""C:\Program Files\GNU ARM Eclipse\Build Tools\2.8-201611221915\bin\sed" 's/^Q(.*)/"&"/' " exited with code 1. (TaskId:21)
The command was translated into the originally intended sed 's/^Q(.*)/"&"/'
However, there now appears to be an issue with cmd.exe.
With respect to cmd.exe, what part of that command doesn't it like?
Is it the single/double quote? The ampersand?
I'm using the table from here.
回答1:
The shell interpreters on Unix/Linux/Mac support at least three types of argument strings:
- None quoted argument strings for simple strings not containing a space character or a character with a special meaning for shell interpreter.
"quoted argument strings for strings containing a space character or a character with a special meaning for shell interpreter not containing".'quoted argument strings for strings containing a space character or a character with a special meaning for shell interpreter not containing'.
The Windows command line interpreter supports only two types of argument strings.
- None quoted argument strings for simple strings not containing a space character or one of these characters:
&()[]{}^=;!'+,`~|<>. "quoted argument strings for strings containing a space character or a character with a special meaning for Windows command interpreter not containing".
For that reason a regular expression string as argument string containing " is problematic on Windows as there is no alternate solution for quoting the argument string as on *nix/Mac shells.
A solution for regular expressions in Perl syntax is using the hexadecimal notation \x22 for double quote character " in the regular expression.
So "s/^Q(.*)/\x22&\x22/" could work on Windows as well as on Linux instead of 's/^Q(.*)/"&"/'.
However, the command line argument strings are finally processed by the called application and it depends on the application and the used compiler how the application interprets the arguments. Therefore it depends always also on the application what finally works. See C code in this answer and the two different results on running this code compiled with three different compilers with a misquoted argument string. Another example for application dependent interpretation of argument string is explained in answer on How to set environment variables with spaces?
For that reason using just s/^Q(.*)/"&"/ without enclosing the regular expression argument string in the single quotes ' works for sed on Windows too.
来源:https://stackoverflow.com/questions/46634889/cmd-sed-command-input-parse-error