使用带有特殊字符的批处理回显
这可能真的很简单,但网上没有答案。我想通过批处理将 XML 行回显到文件中,但它误解了用于重定向的 XML 结束标记“>”。该行如下:
echo <?xml version="1.0" encoding="utf-8" ?> > myfile.xml
有什么方法可以提示批处理解析器不要解释特殊字符串?我使用了双引号,但它也会将它们写入文件! echo 后该文件应如下所示:
<?xml version="1.0" encoding="utf-8" ?>
This maybe really easy but there were no answers for it over the net. I want to echo a XML line via batch into a file but it misunderstands the XML closing tag for redirection ">". The line is as follows:
echo <?xml version="1.0" encoding="utf-8" ?> > myfile.xml
is there any way to give a hint to batch parser not to interpret a special string? I used double-quotes but it writes them to the file as well! The file should look like this after echo:
<?xml version="1.0" encoding="utf-8" ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用
^
转义 shell 元字符:请注意,由于
echo
是内置的 shell,因此它不遵循有关引用的常见约定,因此仅引用参数就会输出引号而不是删除它们。You can escape shell metacharacters with
^
:Note that since
echo
is a shell built-in it doesn't follow the usual conventions regarding quoting, so just quoting the argument will output the quotes instead of removing them.为了使用特殊字符,例如“>”在带有 echo 的 Windows 上,您需要在其前面放置一个特殊的转义字符。
例如,
自 '>' 起将不起作用必须用“^”转义:
另请参阅转义序列。
有一个简短的批处理文件,它打印一组基本的特殊字符及其转义序列。
In order to use special characters, such as '>' on Windows with echo, you need to place a special escape character before it.
For instance
will not work since '>' has to be escaped by '^':
See also escape sequences.
There is a short batch file, which prints a basic set of special character and their escape sequences.
一种简单的解决方案是使用延迟扩展,因为这不会更改任何特殊字符。
编辑:另一种解决方案是使用消失的引号。
此技术使用引号来引用特殊字符
该技巧有效,因为在特殊字符阶段,前导引号
!"!
将保留该行的其余部分(如果没有其他引号)。在延迟扩展阶段,
!"!
将替换为变量"
的内容(单引号是合法名称!)。如果您正在使用禁用的延迟扩展,则可以使用
FOR /F
循环。for /f %%^" in ("""") do echo(%%~"
但作为似乎有点烦人,你也可以构建一个宏。
One easy solution is to use delayed expansion, as this doesn't change any special characters.
EDIT : Another solution is to use a disappearing quote.
This technic uses a quotation mark to quote the special characters
The trick works, as in the special characters phase the leading quotation mark in
!"!
will preserve the rest of the line (if there aren't other quotes).And in the delayed expansion phase the
!"!
will replaced with the content of the variable"
(a single quote is a legal name!).If you are working with disabled delayed expansion, you could use a
FOR /F
loop instead.for /f %%^" in ("""") do echo(%%~" <?xml version="1.0" encoding="utf-8" ?>
But as the seems to be a bit annoying you could also build a macro.
另一种方法:
another method:
输出
>
字符的方法是在其前面加上^
转义字符:将简单打印
The way to output
>
character is to prepend it with^
escape character:will print simply
乔伊的回答对我不起作用。
执行后
我得到这个错误
bash:意外标记“>”附近出现语法错误
这个解决方案对我有用:
另请参阅 http://www.robvanderwoude.com/escapechars.php
The answer from Joey was not working for me.
After executing
I got this error
bash: syntax error near unexpected token `>'
This solution worked for me:
See also http://www.robvanderwoude.com/escapechars.php
这是使用
SET
和FOR /F
的另一种方法,您可以像这样美化它:
Here's one more approach by using
SET
andFOR /F
and you can beautify it like:
转义字符
^
对我来说也不起作用。单引号对我有用(使用ansible脚本)
输出:
the escape character
^
also did not work for me.The single quotes worked for me (using ansible scripting)
output: