转义批处理文件的括号内的括号 [英] Escaping parentheses within parentheses for batch file

查看:31
本文介绍了转义批处理文件的括号内的括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的:

<代码>(@echo 这是一些代码@echo 重要的是要按原样回显@echo 即使我使用括号@echo 的东西(像这样))|剪辑

因此将其复制到剪贴板中.我尝试了以下方法:

<代码>(@echo 这是一些代码@echo 重要的是要按原样回显@echo 即使我使用括号@echo 的东西 ^(像这样^))|剪辑

但这似乎也影响了我的通缉括号,因为我收到了一个错误.为了测试这一点,我简单地完成了:

<代码>(@echo 这是一些代码@echo 重要的是要按原样回显@echo 即使我使用括号@echo 的东西 ^(像这样^)FFF)|剪辑

嘿!这几乎有效,文本一直复制到this"中的s".它不会复制最后一个括号,或者显然是 F.有什么想法吗?

解决方案

很明显你需要三个插入符号 :-)

<代码>(@echo 这是一些代码@echo 重要的是要按原样回显@echo 即使我使用括号@echo 的东西(像这样^^^))|剪辑

为什么?这有点棘手...

解析器首先解析块,将this^^^)部分转义为this^),这里第一次转义了括号.

但是当您使用管道时,整个块将被翻译并传输到一个新的 cmd.exe 实例.
C:Windowssystem32cmd.exe/S/D/c" (@echo 这是一些代码是... & @echo for something (like this^))"

并且在新实例中,需要再次转义右括号.
就这些!

有关更多信息,您可以阅读类似的问题
SO:为什么延迟扩展失败在管道代码块内

This is what I am trying to do:

(
@echo This is some code that is
@echo Important to echo exactly as-is
@echo Even if I use parenthesis
@echo for something (like this)
)|clip

So having that copied into clipboard. I have tried the following:

(
@echo This is some code that is
@echo Important to echo exactly as-is
@echo Even if I use parenthesis
@echo for something ^(like this^)
)|clip

But this seems to affect my WANTED parentheses as well because I receive an error. To test this, I simply done:

(
@echo This is some code that is
@echo Important to echo exactly as-is
@echo Even if I use parenthesis
@echo for something ^(like this^)
FFF
)|clip

Hey! This almost works, the text is copied all the way to the "s" in "this". It does not copy the last parentheses, or obviously the F's. Any ideas?

解决方案

It's obvious that you need three carets there :-)

(
@echo This is some code that is
@echo Important to echo exactly as-is
@echo Even if I use parenthesis
@echo for something (like this^^^)
)|clip

Why? That is a bit tricky...

First the parser parses the block and escapes the part this^^^) to this^), the parenthesis was escaped here the first time.

But as you used a pipe, the complete block will be translated and transfered to a new cmd.exe instance.
C:Windowssystem32cmd.exe /S /D /c" ( @ echo This is some code is ... & @ echo for something (like this^) )"

And in the new instance it is necessary again to escape the closing parenthesis.
That's all!

And for more information you could read a similar question
SO: why does delayed expansion fail when inside a piped block of code

这篇关于转义批处理文件的括号内的括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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