windows 在 CMD 批处理脚本中调用标签时,如何使用 9 个以上的参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8328338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you utilize more than 9 arguments when calling a label in a CMD batch-script?
提问by Mechaflash
I would like to know how to call more than 9 argument within a batch script when calling a label. For example, the following shows that I have 12 arguments assigned along with attempting to echo all of them.
我想知道在调用标签时如何在批处理脚本中调用 9 个以上的参数。例如,以下显示我分配了 12 个参数,并试图回显所有参数。
CALL:LABEL "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" "eleven" "twelve"
PAUSE
GOTO:EOF
:LABEL
echo %1
echo %2
echo %3
echo %4
echo %5
echo %6
echo %7
echo %8
echo %9
echo %10
echo %11
echo %12
The output for %10 %11 and %12 ends up being one0 one1 one2. I've tried using curly brackets, brackets, quotations, single quotes around the numbers without any luck.
%10 %11 和 %12 的输出最终为 one0 one1 one2。我试过在数字周围使用大括号、方括号、引号、单引号,但没有任何运气。
回答by kapex
Use the shift
commandif you want to work with more 9 parameters.
(actually more than 10 parameters if you count the %0
parameter)
如果要使用更多 9 个参数,请使用该shift
命令。
(如果算上参数的话,实际上是10多个%0
参数)
You can [...] use the shift command to create a batch file that can accept more than 10 batch parameters. If you specify more than 10 parameters on the command line, those that appear after the tenth (%9) will be shifted one at a time into %9.
您可以 [...] 使用 shift 命令创建一个批处理文件,该文件可以接受 10 个以上的批处理参数。如果在命令行中指定的参数超过 10 个,则出现在第 10 个 (%9) 之后的参数将一次移一个到 %9。
You can either use a loop, store the variables before shifting, or do it quick like this:
您可以使用循环,在移位之前存储变量,或者像这样快速执行:
@echo off
CALL:LABEL "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" "eleven" "twelve"
PAUSE
GOTO:EOF
:LABEL
:: print arguments 1-9
echo %1
echo %2
echo %3
echo %4
echo %5
echo %6
echo %7
echo %8
echo %9
:: print arguments 10-11
shift
shift
echo %8
echo %9
:: print argument 13
shift
echo %9
You can replace the shift commands with a loop in case you have many arguments. The following for loop executes shift
nine times, so that %1
will be the tenth argument.
如果您有很多参数,您可以用循环替换 shift 命令。下面的 for 循环执行shift
九次,所以这%1
将是第十个参数。
@for /L %%i in (0,1,8) do shift
回答by kkk
This is another way to use the shift
command.
这是使用shift
命令的另一种方式。
Note in this case you can use a variable number of parameters.
请注意,在这种情况下,您可以使用可变数量的参数。
@ECHO OFF
CALL:LABEL "one" "two" "three" "four" "five" "six" "seven" "eight" "nine" "ten" "eleven" "twelve"
PAUSE
GOTO:EOF
:LABEL
echo %1
shift
if not [%1]==[] GOTO LABEL
回答by SeldomNeedy
Assuming we're using a recent-ish version of CMD here, I'm pretty shocked to find no one has posted the following, which allows an arbitrary number of arguments to be processed easily without ever using the clunky shift
command:
假设我们在这里使用的是最新版本的 CMD,我很震惊地发现没有人发布以下内容,它允许在不使用笨拙shift
命令的情况下轻松处理任意数量的参数:
rem test.bat
call :subr %*
exit /b
:subr
for %%A in (%*) do (
echo %%A
)
exit /b
You can also do this same technique right in "main" as well.
您也可以在“主要”中执行相同的技术。
This way, you don't eat up your arguments as you process them, and there is no need for shift
, meaning you can consistently loop through the argument-list more than once if you have complicated calling options and it happens to make your script's logic simpler.
这样,您在处理参数时不会吃掉它们,并且不需要 for shift
,这意味着如果您有复杂的调用选项并且它恰好使您的脚本逻辑,您可以持续多次循环遍历参数列表更简单。
Obviously, if doloop through more than once, it increases computational complexity in exchange for legibility, but neither Bash nor CMD were really built for great speed (as far as I've ever heard) so there's little point in trying to optimize by doing setup all-in-one-go assuming nis any less than 100 or so.
显然,如果做环通不止一次,它增加了计算的复杂性,以换取可读性,但既不猛砸也不CMD真的建了极快的速度(据我听说过),这样就在做试图优化小点假设n小于 100 左右,设置多合一。
Some sample output:
一些示例输出:
C:\tmp>test.bat one two three four five six seven eight nine ten eleven
one
two
three
four
five
six
seven
eight
nine
ten
eleven
edit- On the off chance anyone is processing arguments against another list and part of the work is nested, it's important that an intermediary CALL
be used to allow the current argument being processed to be transferred into the inner loop; simply doing set intermediary=%%OUTER
appears to simply set intermediary
to a an empty string, so it's next simplest to do this:
编辑- 如果有人正在处理针对另一个列表的参数并且部分工作是嵌套的,那么使用中介CALL
来允许将正在处理的当前参数传输到内部循环中是很重要的;简单地做set intermediary=%%OUTER
似乎只是设置intermediary
为一个空字符串,所以它是下一个最简单的方法:
setlocal enabledelayedexpansion
rem ^ in my experience, it's always a good idea to set this if for-loops are involved
for /f %%L in (file.txt) do (
call :inner %%L
)
exit /b
:inner
for %%A in (%*) do (
if %%A EQU %~1 call dosomething.bat %~1
)
exit /b
edit 2- Also, if for whatever reason you want to add the shift
approach into the mix here – perhaps by trying to pass all arguments other than the 1st to a subroutine by using a shift
and then using call :subroutine %*
– note that it won't workbecause %*
actually gives you all of the original arguments in order, ignorant of any shifting you have done.
It's a shame because there's no native syntax (that I know of) that can group all arguments aftera certain one, as one might expect say %3*
to do.
编辑 2- 此外,如果出于某种原因您想将shift
方法添加到此处的混合中 - 也许通过尝试将第一个参数以外的所有参数传递给子例程shift
,然后使用a然后使用call :subroutine %*
- 请注意,它不起作用,因为%*
实际上按顺序为您提供所有原始参数,不知道您所做的任何更改。
这是一种耻辱,因为没有本机语法(据我所知)可以将所有参数分组到某个参数之后,正如人们所期望的%3*
那样。
回答by Laf
You cannot have more than 10 (0 through 9) accessible arguments (%0 being the batchfile itself) in a batch file. However, using the shift
command will allow you to "left-shift" your arguments, and access those arguments beyond the 9th one. If you do it three times, you should end up with %7, %8 and %9 containing "ten", "eleven" and "twelve".
一个批处理文件中不能有超过 10 个(0 到 9)个可访问的参数(%0 是批处理文件本身)。但是,使用该shift
命令将允许您“左移”您的参数,并访问第 9 个参数之后的那些参数。如果执行 3 次,则应以包含“十”、“十一”和“十二”的 %7、%8 和 %9 结束。
回答by Hashbrown
Just throwing my hat in the ring if it helps anyone
如果对任何人有帮助,请把我的帽子扔进戒指
Set a=%1 & Set b=%2 & Set c=%3 & Set d=%4 & Set e=%5 & Set f=%6 & Set g=%7 & Set h=%8 & Set i=%9
Shift & Shift & Shift & Shift & Shift & Shift & Shift & Shift & Shift
Set j=%1 & Set k=%2 & Set l=%3 & Set m=%4 & Set n=%5 & Set o=%6 & Set p=%7 & Set q=%8 & Set r=%9
Shift & Shift & Shift & Shift & Shift & Shift & Shift & Shift & Shift
Set s=%1 & Set t=%2 & Set u=%3 & Set v=%4 & Set w=%5 & Set x=%6 & Set y=%7 & Set z=%8
I just put this at the top of the .bat
or subroutine that will use >9 args, then you can use %a%
through %z%
instead of %1
to the non-existent %26
.
我只是把它放在.bat
将使用 >9 args的or 子例程的顶部,然后你可以使用%a%
through%z%
而不是%1
不存在的%26
.
It's pretty ugly, originally I had it all one one line à la
太丑了,原来我一行一行都写了
Set a=%1 & Shift & Set b=%1 & Shift &...
That way it's only on one line and gets hidden behind the edge of the editor pretty swiftly, but apparently Shift
won't take effect until the next true line; all the variables were set to the first parameter.
这样它只在一行上并且很快地隐藏在编辑器的边缘后面,但显然Shift
直到下一个真正的行才会生效;所有变量都设置为第一个参数。
If you're wondering why do this instead of the loops, unless someone shows me you can create a sort of map or array, I needed to use the variables in a one-line command, not echo
(or do whatever) with them one at a time:
如果你想知道为什么要这样做而不是循环,除非有人告诉我你可以创建一种映射或数组,我需要在一行命令中使用变量,而不是echo
(或做任何事情)在一个时间:
::example; concatenate a bunch of files
Set output=%1
Shift
<insert above behemoth>
type %a% %b% %c% %d% %e% %f% 2>NUL > %output%
Damn this was so much easier in bash
该死的,这在 bash 中容易得多
cat ${@:2} > ""