Windows cmd将一个命令的输出作为参数传递给另一命令 [英] Windows cmd pass output of one command as parameter to another
问题描述
在Linux中,可能无法做到这一点:
In linux it is possible t do this:
git diff $(git status -s -b | sed -n '2p' | cut -d' ' -f2-)
或更简单的情况
ls $(pwd)
问题是如何在Windows中实现相同的目标? (不使用批处理文件,在命令提示符下使用一个衬板).并非所有命令都支持管道,因此我们如何评估一个并将结果作为参数传递给另一个?
The question is how can I achieve the same in windows? (not using a batch file, a one liner in command prompt). Not all commands support piping so how can we evaluate one and pass result as parameter to another?
我已经尝试了管道和<
和>
,但是没有用.
I've tried piping and <
and >
but none work.
git diff < (git status -s -b | sed -n '2p' | cut -d' ' -f2-)
尝试自己尝试获取文件.而且|
也不起作用,因为git diff
不支持
Try that yourself it expects a file. And |
doesn't work either as git diff
doesn't support it
git status -s -b | sed -n '2p' | cut -d' ' -f2- | git diff // results in illegal seek
推荐答案
cmd
中没有$
运算符.
重定向运算符(<
,>
,>>
)期望文件或流句柄.
管道|
将命令的标准输出传递到另一个命令的标准输入中.
There is no $
operator in cmd
.
Redirection operators (<
, >
, >>
) expect files or stream handles.
A pipe |
passes the standard output of a command into the standard input of another one.
for /F
循环可以捕获命令的输出,并将其提供给变量引用(在示例中为%A
);请参阅以下代码:
A for /F
loop however is capable of capturing the output of a command and providing it in a variable reference (%A
in the example); see the following code:
for /F "usebackq delims=" %A in (`git status -s -b ^| sed -n '2p' ^| cut -d' ' -f2-`) do git diff %A
这篇关于Windows cmd将一个命令的输出作为参数传递给另一命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!