Windows cmd 将一个命令的输出作为参数传递给另一个 [英] Windows cmd pass output of one command as parameter to another

查看:49
本文介绍了Windows cmd 将一个命令的输出作为参数传递给另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 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.

A 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆