Windows 如何修复管线延迟扩展条件的批处理脚本错误?
我制作了一个CMD批处理脚本用于Windows7 我必须使用管道条件 但是我的批处理脚本并没有像我期望的那样工作 为什么不在管线中的脚本下工作 下面的Windows 如何修复管线延迟扩展条件的批处理脚本错误?,windows,batch-file,cmd,Windows,Batch File,Cmd,我制作了一个CMD批处理脚本用于Windows7 我必须使用管道条件 但是我的批处理脚本并没有像我期望的那样工作 为什么不在管线中的脚本下工作 下面的echo |语法是管道必须具备的虚拟条件 不平衡报价是必要条件 我必须在不使用setlocalenabledelayedexpansion的情况下完成 我只能使用cmd.exe/v:on/c“…” 结果是: !z!" " 为什么不使用pipe operator处理两个批处理命令行 编辑-由dbenham解释: !z!" " 这个无意义的命令只
echo |
语法是管道必须具备的虚拟条件
不平衡报价是必要条件
我必须在不使用setlocalenabledelayedexpansion
的情况下完成
我只能使用cmd.exe/v:on/c“…”
结果是:
!z!"
"
为什么不使用pipe operator处理两个批处理命令行
编辑-由dbenham解释:
!z!"
"
这个无意义的命令只是一个基本的测试平台,它演示了OP存在的问题
OP希望管道的右侧是一个CMD.EXE进程,该进程定义一个包含奇数引号的变量,然后回显结果值。这个问题写得不好,但它是一个有趣的问题。我留下了原来的问题,以防万一我的解释不正确。听起来很像家庭作业。或者一个谜语 假设最后一个(因为第一个是离题的): 应符合以下要求:
echo |必须使用语法。
检查
不平衡报价是必需的。
检查
不使用SETLOCAL ENABLEDELAYEDEXPANSION。
检查
使用cmd.exe/v:on/c“…”
检查
结果是:
Car and" Airplane are Machines
(更换以证明它显示的是第二个回波,而不是第一个回波)您的问题与管道无关。您的问题是如何正确地转义命令字符串,以便赋值在值中包含奇数个引号,然后ECHO命令正确地显示结果值。如果移除管道并仅单独执行CMD.EXE命令,则会出现完全相同的问题 在我提供解决方案之前,您肯定应该先深入了解复杂管道操作的复杂性 有几件事使你的问题变得复杂: 1) 命令字符串被多次解析-首先由主批处理脚本解析,在这种情况下,必须确保
&
被引用或转义,以便传递给CMD.EXE。第二次它是由CMD.EXE本身解析的,这一次您需要在赋值中以某种方式包含一个引号,但是现在不能转义或引用&
,以便它正确地触发ECHO命令来显示结果
2) 在执行之前,CMD.EXE将删除包含命令字符串的最外层引号。但是知道不需要外部引号是很有帮助的:-)您只需找到正确的转义和引号序列
有很多解决方案,而且完全可以预测。然而,当你试图追踪它的工作原理时,它很容易让你的头爆炸;-)
下面的所有工作。我在输出周围添加了方括号,以证明赋值中没有不需要的字符。不需要的忽略文本只是为了演示引用的赋值如何忽略最后一个引用后面的文本。可以简单地删除被忽略的文本
@echo off
:: Without quotes around the command
echo | cmd.exe /v:on /c set z=Car^^" and Airplane is Machine&echo [!z!]
echo | cmd.exe /v:on /c set z=Car^^^" and Airplane is Machine^&echo [!z!]
:: With quotes around the command
echo | cmd.exe /v:on /c "set z=Car^" and Airplane is Machine^&echo [!z!]"
echo | cmd.exe /v:on /c ^"set z=Car^^" and Airplane is Machine&echo [!z!]"
:: With quotes around the assignment such that text after the last quote is ignored
:: Without quotes around the command
echo | cmd.exe /v:on /c set "z=Car" and Airplane is Machine^^" IGNORED & echo [!z!]
echo | cmd.exe /v:on /c set ^^"z=Car" and Airplane is Machine" IGNORED & echo [!z!]
echo | cmd.exe /v:on /c set "z=Car" and Airplane is Machine^^^" IGNORED ^& echo [!z!]
echo | cmd.exe /v:on /c set ^^"z=Car" and Airplane is Machine^" IGNORED ^& echo [!z!]
:: With quotes around the assignment such that text after the last quote is ignored
:: With quotes around the command
echo | cmd.exe /v:on /c "set "z=Car" and Airplane is Machine^" IGNORED ^& echo [!z!]"
echo | cmd.exe /v:on /c "set ^"z=Car" and Airplane is Machine" IGNORED ^& echo [!z!]"
echo | cmd.exe /v:on /c "set "z=Car^" and Airplane is Machine^^" IGNORED & echo [!z!]"
echo | cmd.exe /v:on /c "set ^"z=Car^" and Airplane is Machine" IGNORED & echo [!z!]"
--结果--
当然,您的实际代码明显不同。如果您在尝试将这些概念应用于实际问题时还有其他工作要做,我不会感到惊讶。祝你好运 MSDOS是一个古老的东西,已经有20年没有使用了。你用的是什么版本?此外,代码本身也没有意义,您能描述一下它应该做什么吗?尝试在单独的行(在@echo off下面)上添加“setlocal enabledelayedexpansion”延迟扩展通常只在
for
循环中起作用。请注意你的格式。您已阻止引号代码和代码格式文本。引号不平衡。在和之后的报价是否是有意的?echo |
应该做什么?为什么不平衡的引号是必需的条件?预期的结果是什么?
@echo off
:: Without quotes around the command
echo | cmd.exe /v:on /c set z=Car^^" and Airplane is Machine&echo [!z!]
echo | cmd.exe /v:on /c set z=Car^^^" and Airplane is Machine^&echo [!z!]
:: With quotes around the command
echo | cmd.exe /v:on /c "set z=Car^" and Airplane is Machine^&echo [!z!]"
echo | cmd.exe /v:on /c ^"set z=Car^^" and Airplane is Machine&echo [!z!]"
:: With quotes around the assignment such that text after the last quote is ignored
:: Without quotes around the command
echo | cmd.exe /v:on /c set "z=Car" and Airplane is Machine^^" IGNORED & echo [!z!]
echo | cmd.exe /v:on /c set ^^"z=Car" and Airplane is Machine" IGNORED & echo [!z!]
echo | cmd.exe /v:on /c set "z=Car" and Airplane is Machine^^^" IGNORED ^& echo [!z!]
echo | cmd.exe /v:on /c set ^^"z=Car" and Airplane is Machine^" IGNORED ^& echo [!z!]
:: With quotes around the assignment such that text after the last quote is ignored
:: With quotes around the command
echo | cmd.exe /v:on /c "set "z=Car" and Airplane is Machine^" IGNORED ^& echo [!z!]"
echo | cmd.exe /v:on /c "set ^"z=Car" and Airplane is Machine" IGNORED ^& echo [!z!]"
echo | cmd.exe /v:on /c "set "z=Car^" and Airplane is Machine^^" IGNORED & echo [!z!]"
echo | cmd.exe /v:on /c "set ^"z=Car^" and Airplane is Machine" IGNORED & echo [!z!]"
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]
[Car" and Airplane is Machine]