使用带有特殊字符的批处理回显

发布于 2024-12-03 12:26:35 字数 312 浏览 1 评论 0原文

这可能真的很简单,但网上没有答案。我想通过批处理将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

我纯我任性 2024-12-10 12:26:36

您可以使用 ^ 转义 shell 元字符:

echo ^<?xml version="1.0" encoding="utf-8" ?^> > myfile.xml

请注意,由于 echo 是内置的 shell,因此它不遵循有关引用的常见约定,因此仅引用参数就会输出引号而不是删除它们。

You can escape shell metacharacters with ^:

echo ^<?xml version="1.0" encoding="utf-8" ?^> > myfile.xml

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.

脱离于你 2024-12-10 12:26:36

为了使用特殊字符,例如“>”在带有 echo 的 Windows 上,您需要在其前面放置一个特殊的转义字符。

例如,

echo A->B

自 '>' 起将不起作用必须用“^”转义:

 echo A-^>B

另请参阅转义序列
在此处输入图像描述

有一个简短的批处理文件,它打印一组基本的特殊字符及其转义序列。

In order to use special characters, such as '>' on Windows with echo, you need to place a special escape character before it.

For instance

echo A->B

will not work since '>' has to be escaped by '^':

 echo A-^>B

See also escape sequences.
enter image description here

There is a short batch file, which prints a basic set of special character and their escape sequences.

清醇 2024-12-10 12:26:36

一种简单的解决方案是使用延迟扩展,因为这不会更改任何特殊字符。

set "line=<?xml version="1.0" encoding="utf-8" ?>"
setlocal EnableDelayedExpansion
(
  echo !line!
) > myfile.xml

编辑:另一种解决方案是使用消失的引号

此技术使用引号来引用特殊字符

@echo off
setlocal EnableDelayedExpansion
set ""="
echo !"!<?xml version="1.0" encoding="utf-8" ?>

该技巧有效,因为在特殊字符阶段,前导引号!"! 将保留该行的其余部分(如果没有其他引号)。
在延迟扩展阶段,!"! 将替换为变量 " 的内容(单引号是合法名称!)。

如果您正在使用禁用的延迟扩展,则可以使用 FOR /F 循环。

for /f %%^" in ("""") do echo(%%~"

但作为似乎有点烦人,你也可以构建一个宏。

set "print=for /f %%^" in ("""") do echo(%%~""

%print%<?xml version="1.0" encoding="utf-8" ?>
%print% Special characters like &|<>^ works now without escaping

One easy solution is to use delayed expansion, as this doesn't change any special characters.

set "line=<?xml version="1.0" encoding="utf-8" ?>"
setlocal EnableDelayedExpansion
(
  echo !line!
) > myfile.xml

EDIT : Another solution is to use a disappearing quote.

This technic uses a quotation mark to quote the special characters

@echo off
setlocal EnableDelayedExpansion
set ""="
echo !"!<?xml version="1.0" encoding="utf-8" ?>

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.

set "print=for /f %%^" in ("""") do echo(%%~""

%print%<?xml version="1.0" encoding="utf-8" ?>
%print% Special characters like &|<>^ works now without escaping
苦行僧 2024-12-10 12:26:36

另一种方法:

@echo off

for /f "useback delims=" %%_ in (%0) do (
  if "%%_"=="___ATAD___" set $=
  if defined $ echo(%%_
  if "%%_"=="___DATA___" set $=1
)
pause
goto :eof

___DATA___
<?xml version="1.0" encoding="utf-8" ?>
 <root>
   <data id="1">
      hello world
   </data>
 </root>
___ATAD___


rem # 
rem # 

another method:

@echo off

for /f "useback delims=" %%_ in (%0) do (
  if "%%_"=="___ATAD___" set $=
  if defined $ echo(%%_
  if "%%_"=="___DATA___" set $=1
)
pause
goto :eof

___DATA___
<?xml version="1.0" encoding="utf-8" ?>
 <root>
   <data id="1">
      hello world
   </data>
 </root>
___ATAD___


rem # 
rem # 
屌丝范 2024-12-10 12:26:36

输出 > 字符的方法是在其前面加上 ^ 转义字符:

echo ^>

将简单打印

>

The way to output > character is to prepend it with ^ escape character:

echo ^>

will print simply

>
背叛残局 2024-12-10 12:26:36

乔伊的回答对我不起作用。
执行后

  echo ^<?xml version="1.0" encoding="utf-8" ?^> > myfile.xml

我得到这个错误
bash:意外标记“>”附近出现语法错误

这个解决方案对我有用:

 echo "<?xml version=\"1.0\" encoding=\"utf-8\">" > myfile.txt

另请参阅 http://www.robvanderwoude.com/escapechars.php

The answer from Joey was not working for me.
After executing

  echo ^<?xml version="1.0" encoding="utf-8" ?^> > myfile.xml

I got this error
bash: syntax error near unexpected token `>'

This solution worked for me:

 echo "<?xml version=\"1.0\" encoding=\"utf-8\">" > myfile.txt

See also http://www.robvanderwoude.com/escapechars.php

清君侧 2024-12-10 12:26:36

这是使用 SETFOR /F 的另一种方法

@echo off

set "var=<?xml version="1.0" encoding="utf-8" ?>"

for /f "tokens=1* delims==" %%a in ('set var') do echo %%b

,您可以像这样美化它:

@echo off
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set "print{[=for /f "tokens=1* delims==" %%a in ('set " & set "]}=') do echo %%b"
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


set "xml_line.1=<?xml version="1.0" encoding="utf-8" ?>"
set "xml_line.2=<root>"
set "xml_line.3=</root>"

%print{[% xml_line %]}%

Here's one more approach by using SET and FOR /F

@echo off

set "var=<?xml version="1.0" encoding="utf-8" ?>"

for /f "tokens=1* delims==" %%a in ('set var') do echo %%b

and you can beautify it like:

@echo off
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
set "print{[=for /f "tokens=1* delims==" %%a in ('set " & set "]}=') do echo %%b"
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::


set "xml_line.1=<?xml version="1.0" encoding="utf-8" ?>"
set "xml_line.2=<root>"
set "xml_line.3=</root>"

%print{[% xml_line %]}%
君勿笑 2024-12-10 12:26:36

转义字符 ^ 对我来说也不起作用。
单引号对我有用(使用ansible脚本)

shell: echo  '{{ jobid.content }}'

输出:

 {
    "changed": true,
    "cmd": "echo  '<response status=\"success\" code=\"19\"><result><msg><line>query job enqueued with jobid 14447</line></msg><job>14447</job></result></response>'",
    "delta": "0:00:00.004943",
    "end": "2020-07-31 08:45:05.645672",
    "invocation": {
        "module_args": {
            "_raw_params": "echo  '<response status=\"success\" code=\"19\"><result><msg><line>query job enqueued with jobid 14447</line></msg><job>14447</job></result></response>'",
            "_uses_shell": true,
            "argv": null,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "stdin_add_newline": true,
            "strip_empty_ends": true,
            "warn": true
        }
    },
    "rc": 0,
    "start": "2020-07-31 08:45:05.640729",
    "stderr": "",
    "stderr_lines": [],
    "stdout": "<response status=\"success\" code=\"19\"><result><msg><line>query job enqueued with jobid 14447</line></msg><job>14447</job></result></response>",
    "stdout_lines": [
        "<response status=\"success\" code=\"19\"><result><msg><line>query job enqueued with jobid 14447</line></msg><job>14447</job></result></response>"
    ]

the escape character ^ also did not work for me.
The single quotes worked for me (using ansible scripting)

shell: echo  '{{ jobid.content }}'

output:

 {
    "changed": true,
    "cmd": "echo  '<response status=\"success\" code=\"19\"><result><msg><line>query job enqueued with jobid 14447</line></msg><job>14447</job></result></response>'",
    "delta": "0:00:00.004943",
    "end": "2020-07-31 08:45:05.645672",
    "invocation": {
        "module_args": {
            "_raw_params": "echo  '<response status=\"success\" code=\"19\"><result><msg><line>query job enqueued with jobid 14447</line></msg><job>14447</job></result></response>'",
            "_uses_shell": true,
            "argv": null,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "stdin_add_newline": true,
            "strip_empty_ends": true,
            "warn": true
        }
    },
    "rc": 0,
    "start": "2020-07-31 08:45:05.640729",
    "stderr": "",
    "stderr_lines": [],
    "stdout": "<response status=\"success\" code=\"19\"><result><msg><line>query job enqueued with jobid 14447</line></msg><job>14447</job></result></response>",
    "stdout_lines": [
        "<response status=\"success\" code=\"19\"><result><msg><line>query job enqueued with jobid 14447</line></msg><job>14447</job></result></response>"
    ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文