将输出重定向到 Windows 中另一个命令的命令行参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1425807/
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
Redirecting output to a command-line argument of another command in Windows
提问by Rabarberski
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)
在 Windows 中,如何将脚本/命令的输出用作另一个脚本的参数?(管道 | 在这里不起作用,因为其他脚本不会从标准输入中读取)
Too clarify: I have AnotherScript that needs an argument arg, e.g.:
太澄清了:我有需要参数 arg 的另一个脚本,例如:
AnotherScript 12
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
现在我希望参数(示例中的 12)来自脚本的输出,称为 ScriptB。所以我想要一些类似的东西
AnotherScript (ScriptB)
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:
AnotherScript 实际上是一个需要参数的 python 脚本,而 ScriptB 是一个 cygwin bash 脚本,它产生一些数字,所以我想使用它的方式是这样的:
c:\Python26\python.exe AnotherScript (c:\cygwin|bin|bash --login -i ./ScriptB)
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.
感谢您的回答。但是,考虑到需要费力的“for”构造,我重写了 AnotherScript 以从标准输入中读取。这似乎是一个更好的解决方案。
回答by Joey
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
命令来捕获命令的输出:
for /f "usebackq delims=" %%x in (`ScriptB`) do set args=%%x
then you can use that output in another command:
然后您可以在另一个命令中使用该输出:
AnotherScript %args%
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:
不过,这将导致%args%
包含来自ScriptB
输出的最后一行。如果它只返回一行,您可以将其合并为一行:
for /f "usebackq delims=" %%x in (`ScriptB`) do AnotherScript %%x
When used outside a batch file you have to use %x
instead of %%x
.
在批处理文件之外使用时%x
,您必须使用%%x
.
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:
但是,如果ScriptB
返回多于一行,AnotherScript
则针对这些行中的每一行运行。您可以通过在第一次循环迭代后中断来规避这一点——尽管只能在批处理文件中:
for /f "usebackq delims=" %%x in (`ScriptB`) do AnotherScript %%x & goto :eof
回答by Frank Bollack
You could save the output of your first script to an environment variable and the use its content as command line parameter to the second script, if that is what you want.
您可以将第一个脚本的输出保存到环境变量中,并将其内容用作第二个脚本的命令行参数(如果这是您想要的)。
Something like this:
像这样的东西:
for /f "delims=" %a in ('first_script.cmd') do @set ouput=%a
second_script %ouput%
回答by Sven Hecht
Save the data into a temporary file?
将数据保存到临时文件中?
or use a named pipe instead of a pipe on the standard in and output.
或者在标准输入和输出上使用命名管道而不是管道。
or call the other script from the first one and transfer the data via command arguments
或从第一个脚本调用另一个脚本并通过命令参数传输数据
回答by reinierpost
Aren't you making things way more complicated than necessary?
你不是让事情变得比必要的复杂吗?
Call AnotherScript from bash. Where necessary, install Cygwin just so you can do this.
从 bash 调用另一个脚本。如有必要,请安装 Cygwin,以便您可以执行此操作。