输出重定向到Windows中的另一个命令的命令行参数
我怎样才能在Windows中,使用脚本/命令的输出作为另一个脚本的参数?
(A管|不会在这里工作,因为其他脚本无法从标准输入读取)
How can I, in Windows, use the output of a script/command as an argument for another script? (A pipe | will not work here, since that other script doesn't read from the standard input)
太澄清:我有AnotherScript需要一个参数arg的,例如:
Too clarify: I have AnotherScript that needs an argument arg, e.g.:
AnotherScript 12
现在我想要的参数(12中的例子)来自一个脚本输出,称之为ScriptB。所以我想沿着东西线
now I want the argument (12 in the example) to come from the output of a script, call it ScriptB. So I would like something along the lines of
AnotherScript (ScriptB)
该AnotherScript实际上是需要一个参数一个Python脚本,ScriptB是一个cygwin bash脚本,产生一些数字,所以我想用它的方式是一样的东西:
The AnotherScript is actually a python script that requires an argument, and ScriptB is a cygwin bash script that produces some number, so the way I would like to use it is something like:
c:\Python26\python.exe AnotherScript (c:\cygwin|bin|bash --login -i ./ScriptB)
感谢您的答案。然而,鉴于费力'为'结构需要,我已经重写AnotherScript从标准输入读取。这似乎是一个更好的解决方案。
Thanks for the answers. However, given the laborious 'for' construct required, I've rewritten AnotherScript to read from the standard input. That seems like a better solution.
请注意:所有这一切都需要的命令是在一个批处理文件。因此,双%
迹象。
Note: All this requires the commands to be in a batch file. Hence the double %
signs.
您可以使用为
命令捕获命令的输出:
You can use the for
command to capture the output of the command:
for /f "usebackq delims=" %%x in (`ScriptB`) do set args=%%x
,那么你可以使用输出另一个命令:
then you can use that output in another command:
AnotherScript %args%
这将导致%ARGS%
来包含 ScriptB
的输出的最后一行,虽然。如果它只返回一行就可以擀成一条线这样的:
This will cause %args%
to contain the last line from ScriptB
's output, though. If it only returns a single line you can roll this into one line:
for /f "usebackq delims=" %%x in (`ScriptB`) do AnotherScript %%x
在一个批处理文件外使用,你必须使用%X
而不是 %% X
。
When used outside a batch file you have to use %x
instead of %%x
.
但是,如果 ScriptB
返回多个行, AnotherScript
运行,为每个行。您可以绕过这个,虽然只在一个批处理文件,由破后的第一个循环迭代:
However, if ScriptB
returns more than one line, AnotherScript
runs for each of those lines. You can circumvent this—though only within a batch file—by breaking after the first loop iteration:
for /f "usebackq delims=" %%x in (`ScriptB`) do AnotherScript %%x & goto :eof