简体   繁体   English

批处理文件等效于带引号的Unix参数扩展

[英]Batch file equivalent of Unix parameter expansion with quotes

There have been a lot of questions asked and answered about batch file parameters with regards to the %* , but I haven't found an answer for this. 关于%* ,有关批处理文件参数有很多问题要问和回答,但是我还没有找到答案。

Is there an equivalent syntax in batch files that can perform the same behavior as "$@" in Unix? 批处理文件中是否存在等效的语法,可以执行与Unix中的“ $ @”相同的行为?

Some context: 一些背景:

@echo off
set MYPATH=%~dp0
set PYTHON=%MYPATH%..\python\python
set BASENAME=%~n0
set XTPY=%MYPATH%..\SGTools\bin\%BASENAME%.py
"%PYTHON%" "%XTPY%" %*

This is the .bat file that is being used a proxy to call a Python script. 这是.bat文件,正在使用该文件作为代理来调用Python脚本。 So I am passing all the parameters (except the script name) to the Python script. 因此,我将所有参数(脚本名称除外)传递给Python脚本。 This works fine until there is a parameter in quotes and/or contains spaces. 直到引号中包含参数和/或包含空格之前,此方法都可以正常工作。

In shell scripts you can use "$@" to take each parameter and enclose it in quotes. 在shell脚本中,可以使用“ $ @”来获取每个参数并将其用引号引起来。 Is there something I can do to replicate this process? 我可以做些什么来复制此过程吗?

Example calls: 示例调用:

xt -T sg -t "path with possible spaces" -sum "name with spaces" -p <tool_name> -o lin32 lin64 win32 <lots of other options with possibilities of spaces>

The command/file xt simply contains the code listed above, because the actual executable is Python code in a different folder. 命令/文件xt仅包含上面列出的代码,因为实际的可执行文件是其他文件夹中的Python代码。 So the point is to create a self-contained package where you only add one directory (xbin directory) to your path. 因此,关键是要创建一个自包含的程序包,在该程序包中,您只需向路径添加一个目录(xbin目录)。

I'm not sure what the cleanest solution is, but here is how I worked around the problem: 我不确定最干净的解决方案是什么,但是这是我解决该问题的方法:

setlocal EnableDelayedExpansion
for %%i in (%*) do set _args= !_args! "%%~i"
echo %_args%

%_args% will now contain a quoted list of each individual parameter. %_args%现在将包含每个单独参数的带引号的列表。 For example, if you called the batch file as follows: 例如,如果您按以下方式调用批处理文件:

MYBATFILE "test'1'file" "test'2'file" "test 3 file"`

echo %_args% 回声%_args%

will produce the original quoted input. 将产生原始引用的输入。

I needed this for CMD files that take unfriendly file or directory names and pass them to Cygwin Bash shell scripts which do the heavy lifting, but I couldn't afford to have the embedded single quotes or spaces lost in the transition. 对于采用不友好文件或目录名称并将其传递给Cygwin Bash Shell脚本的CMD文件,我需要这样做,而Cygwin Bash Shell脚本会承担繁重的工作,但是我负担不起在转换过程中丢失嵌入式单引号或空格。

Note the ~i in %%~i% which is necessary to remove quotes before we apply quotes. 注意了〜我 %%〜我%,这是必要的,才适用引号去掉引号。 When a parameter containing spaces is passed (eg, "test 3 file" above), the CMD shell will already have applied quotes to it. 当传递包含空格的参数时(例如,上面的“ test 3文件”),CMD Shell将已经对其应用了引号。 The tilde here makes sure that we don't double-quote parameters containing spaces. 此处的代字号可确保我们不对包含空格的参数进行双引号处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM