Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 从批处理调用批处理时,如何使用符号AND传递参数_Batch File_Cmd_Command Prompt - Fatal编程技术网

Batch file 从批处理调用批处理时,如何使用符号AND传递参数

Batch file 从批处理调用批处理时,如何使用符号AND传递参数,batch-file,cmd,command-prompt,Batch File,Cmd,Command Prompt,foo.bat: call qwe.bat "hello && bye" qwe.bat: @echo off set cmd=%~1 echo "%cmd%" 当我执行foo.bat时,我得到一个错误: 'bye' is not recognized as an internal or external command, operable program or batch file. "hello " 那么如何将字符串“hello&&bye”从foo.bat传递到qwe.

foo.bat:

call qwe.bat "hello && bye"
qwe.bat:

@echo off
set cmd=%~1
echo "%cmd%"
当我执行
foo.bat
时,我得到一个错误:

'bye' is not recognized as an internal or external command,
operable program or batch file.
"hello "
那么如何将字符串
“hello&&bye”
foo.bat
传递到
qwe.bat

看起来是ampersands导致了问题批转义规则非常糟糕,但是如果您知道这些规则,那么行为是完全可以预测的

您需要了解问题的信息可以在Windows命令解释器(CMD.EXE)如何解析脚本中找到?在接受答案的第1、2、5和6阶段。但祝你在任何时候都能很快吸收这些信息:-)

有两个基本的设计问题会导致您的问题:-阶段6将所有插入符号加倍,然后重新启动阶段2(实际上是阶段1、1.5和2)。-但第2阶段要求&作为^&而转义。注意,它必须是一个^,而不是两倍

让您的方法发挥作用的唯一方法是在第6阶段插入符号加倍发生后引入^

@echo off
setlocal enableDelayedExpansion
set "ESC=^"

rem Calling with delayed expansion value
set "val=with%%ESC%%&ampersand"
call :Output !val!

rem Calling with a string literal
call :Output with%%ESC%%^&ampersand

exit /b

:Output
set "line=%1"
echo Called: !line!
goto :eof
ESC被定义为保持^。 第一阶段的第一轮将%%ESC%%扩展到%ESC% 第二轮阶段1(由阶段6启动)将%ESC%扩展到^

这完全是不切实际的,尤其是如果你不知道内容会是什么

可靠地将任何值传递给被调用例程的唯一明智策略是通过引用传递。传递包含字符串值的变量的名称,并使用延迟展开在子例程中展开该值

@echo off
setlocal enableDelayedExpansion
set "val=with&ampersand"
call :Output val
exit /b

:Output
set "line=!%~1!"
echo Called: !line!
exit /b

使用推荐的语法定义变量后,
设置“Var=String Value”
,只需使用延迟扩展:

@Echo关闭
设置“Cmnd=%~1”
SetLocal EnableDelayedExpansion
回声!Cmnd!
暂停
端部