cmd-sed命令输入解析错误 [英] cmd - sed command input parse error

查看:169
本文介绍了cmd-sed命令输入解析错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将GNU Makefile转换为MSBuild XML.

I'm translating a GNU Makefile into MSBuild XML.

我有以下sed命令(是较大命令的一部分):

I have the following sed command (part of a larger command):

... | sed 's/^Q(.*)/"&"/' | ...

当我只执行Cygwin中的那个sed部分时,它在没有错误输出的意义上起作用".

When I execute just that sed portion in Cygwin, it "works" in the sense that it doesn't error out.

但是,在将其转换为MSBuild XML之后-用"&'替换了对XML敏感的符号-我得到了错误

However, after I've translated it to MSBuild XML - replaced the XML-sensitive symbols with ", &, ' - I get the error

sed:不支持的命令'

sed: unsupported command '

我确定这不是XML转义问题. Visual Studio构建日志说

I'm sure it's not an issue with XML escaping issues; the Visual Studio build log says

任务参数:Command ="C:\ Program Files \ GNU ARM Eclipse \ Build Tools \ 2.8-201611221915 \ bin \ sed"'s/^ Q(.*)/&"/'(TaskId:21 )

Task Parameter:Command="C:\Program Files\GNU ARM Eclipse\Build Tools\2.8-201611221915\bin\sed" 's/^Q(.*)/"&"/' (TaskId:21)

sed:不支持的命令'(TaskId:21)

sed: unsupported command ' (TaskId:21)

命令"C:\ Program Files \ GNU ARM Eclipse \ Build Tools \ 2.8-201611221915 \ bin \ sed"的s/^ Q(.*)/&"/'退出,代码为1. (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)

该命令已翻译为最初的sed 's/^Q(.*)/"&"/'

The command was translated into the originally intended sed 's/^Q(.*)/"&"/'

但是,cmd.exe现在似乎有问题.

However, there now appears to be an issue with cmd.exe.

相对于cmd.exe,该命令的哪个部分不喜欢?
是单引号还是双引号? &符?

With respect to cmd.exe, what part of that command doesn't it like?
Is it the single/double quote? The ampersand?

我正在从这里使用表 .

推荐答案

Unix/Linux/Mac上的shell解释器至少支持三种类型的参数字符串:

The shell interpreters on Unix/Linux/Mac support at least three types of argument strings:

  1. 对于不包含空格字符或shell解释程序具有特殊含义的简单字符串,没有引号的参数字符串.
  2. "用引号括起来的参数字符串用于包含空格字符或不包含"的shell解释程序具有特殊含义的字符.
  3. '用引号括起来的参数字符串用于包含空格字符或不包含'的shell解释程序具有特殊含义的字符.
  1. None quoted argument strings for simple strings not containing a space character or a character with a special meaning for shell interpreter.
  2. " quoted argument strings for strings containing a space character or a character with a special meaning for shell interpreter not containing ".
  3. ' quoted argument strings for strings containing a space character or a character with a special meaning for shell interpreter not containing '.

Windows命令行解释器仅支持两种类型的参数字符串.

The Windows command line interpreter supports only two types of argument strings.

  1. 对于不包含空格字符或以下字符之一的简单字符串,没有引号的参数字符串:&()[]{}^=;!'+,`~|<>.
  2. "用引号括起来的参数字符串包含空格字符或不包含"的Windows命令解释器具有特殊含义的字符.
  1. None quoted argument strings for simple strings not containing a space character or one of these characters: &()[]{}^=;!'+,`~|<>.
  2. " quoted argument strings for strings containing a space character or a character with a special meaning for Windows command interpreter not containing ".

因此,在Windows上正则表达式字符串作为包含"的参数字符串是有问题的,因为没有替代解决方案可以像在* nix/Mac shell上那样引用参数字符串.

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.

Perl语法中正则表达式的一种解决方案是对正则表达式中的双引号字符"使用十六进制表示法\x22.

A solution for regular expressions in Perl syntax is using the hexadecimal notation \x22 for double quote character " in the regular expression.

因此"s/^Q(.*)/\x22&\x22/"可以在Windows和Linux上运行,而不是's/^Q(.*)/"&"/'.

So "s/^Q(.*)/\x22&\x22/" could work on Windows as well as on Linux instead of 's/^Q(.*)/"&"/'.

但是,命令行参数字符串最终由被调用的应用程序处理,并且取决于应用程序和所使用的编译器,应用程序如何解释参数.因此,最终的工作方式还始终取决于应用程序.请参阅此答案中的 C 代码,以及运行此代码的两个不同结果,这些代码使用三种不同的方式编译带有错误引用的参数字符串的编译器.在如何用空格设置环境变量的答案中解释了依赖于应用程序解释参数字符串的另一个示例.

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?

因此,仅使用s/^Q(.*)/"&"/而不将正则表达式参数字符串括在单引号中'在Windows上也适用于 sed .

For that reason using just s/^Q(.*)/"&"/ without enclosing the regular expression argument string in the single quotes ' works for sed on Windows too.

这篇关于cmd-sed命令输入解析错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆