通过类似开关的参数进行循环的批处理文件?
我正在尝试循环传递给批处理文件的参数。根据参数,我想设置一个变量标志 true 或 false 以便稍后在脚本中使用
所以我的命令是“myscript.bat /u /p /s”
我的代码是:
FOR /f %%a IN ("%*") DO (
IF /I "%%a"=="/u" SET UPDATE=Y
IF /I "%%a"=="/p" SET PRIMARY=Y
IF /I "%%a"=="/s" SET SECONDARY=Y
)
它仅在我有一个参数时才有效,它告诉我它将整个参数列表作为单个参数获取。我尝试过“delims=”但无济于事。关于获得每个间隔参数有什么想法吗?
向其中一个参数添加一个值怎么样?
myscript.bat /u /p /d TEST /s
:loop
IF "%~1"=="" GOTO cont
IF /I "%~1"=="/u" SET UPDATE=Y
IF /I "%~1"=="/p" SET PRIMARY=Y
IF /I "%~1"=="/s" SET SECONDARY=Y
IF /I "%~1"=="/d" SHIFT & SET DISTRO="%~1"
SHIFT & GOTO loop
:cont
但是与最后一个 IF 内联的 SHIFT 实际上并没有改变任何内容。 DISTRO 最终是“/d”而不是“TEST”
I'm trying to loop through the arguments that I am passing to a batch file. Based on the argument, I want to set a variable flag true or false for use later in the script
So my command is "myscript.bat /u /p /s"
And my code is:
FOR /f %%a IN ("%*") DO (
IF /I "%%a"=="/u" SET UPDATE=Y
IF /I "%%a"=="/p" SET PRIMARY=Y
IF /I "%%a"=="/s" SET SECONDARY=Y
)
It only works if i have a single argument, which tells me that it is getting the entire list of arguments as a single argument. I've tried "delims= " but to no avail. Any thoughts on getting each spaced argument?
What about adding a value to one of the params?
myscript.bat /u /p /d TEST /s
:loop
IF "%~1"=="" GOTO cont
IF /I "%~1"=="/u" SET UPDATE=Y
IF /I "%~1"=="/p" SET PRIMARY=Y
IF /I "%~1"=="/s" SET SECONDARY=Y
IF /I "%~1"=="/d" SHIFT & SET DISTRO="%~1"
SHIFT & GOTO loop
:cont
But the SHIFT that comes inline with the last IF doesn't actually shift anything. DISTRO ends up being "/d" instead of "TEST"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你离你原来的作品并不太远,而且因为我不喜欢 GOTO 循环,所以我想我应该发布这个:
它只使用一个参数的原因是过度使用引号。通过将 %* 放在引号中,您可以将整个命令行变成一个标记。此外,FOR 的 /F 变体也不是您想要的。文档可从 FOR /?应该有助于解决问题。
You're not too far off on your original piece, and since I dislike GOTO loops, I thought I'd post this:
The reason it was only working with one parameter is the over-use of quotes. By putting %* in quotes you were making the entire commandline one single token. also, the /F variant of FOR isn't what you were looking for either. The documentation available from FOR /? should help clear things up.
您可以使用 SHIFT、GOTO 和额外的 IF 循环遍历参数,以检查是否没有更多参数需要解析:
有自己的参数时的情况)
UPDATE(解决参数 检查
/d
的 IF 语句确实有效。问题是整行会立即计算,并且%~1
的两个实例都被替换为相同的值,此时为/d
。因此,基本上这种情况下的解决方案是使解释器与
IF /I "%~1"==" 分开评估
。对此可以有多种方法。例如,您可以简单地移动SET DISTRO="%~1"
部分/d”SHIFT & SET DISTRO="%~1"
到下一行,如果%~1
不是/d
则 跳过它:方法可以是为
DISTRO
分配一个特殊值(例如?
),并在遇到/d
时进行切换。然后,在下一行中检查DISTRO
是否具有该特殊值并将其设置为%~1
:You could loop over the arguments using SHIFT, GOTO and an extra IF to check if there are no more parameters to parse:
UPDATE (addressing the case when a parameter has an argument of its own)
The SHIFT in the IF statement that checks for
/d
does work. The issue is that the entire line is evaluated at once and both instances of%~1
get replaced with the same value, which is/d
at that point.So, basically the solution in this case would be to cause the interpreter to evaluate the
SET DISTRO="%~1"
part separately from theIF /I "%~1"=="/d"
. There can be various approaches to this. For instance, you could simply moveSHIFT & SET DISTRO="%~1"
to the next line and skip it if%~1
is not/d
:Another method could be to assign a special value (e.g. a
?
) toDISTRO
and shift when/d
is encountered. Then, on the next line, check ifDISTRO
has that special value and set it to%~1
: