如何使用管道将一个命令的输出重定向到另一个命令的输入?

时间:2013-01-29 01:23:51

标签: windows pipe command-prompt

我有一个程序可以将文本发送到LED标志。

prismcom.exe

使用该程序发送“Hello”:

prismcom.exe usb Hello

现在,我希望使用一个名为Temperature的命令程序。

temperature

假设该程序可以提供计算机的温度。

Your computer is 100 degrees Fahrenheit.

现在,我希望将温度输出写入prismcom.exe:

temperature | prismcom.exe usb

这似乎不起作用。

是的,我已经找了20多分钟的解决方案了。在所有情况下,除了Windows命令行之外,它们都是kludges / hacks或解决方案。

我很欣赏如何将输出从温度传输到prismcom。

谢谢!

编辑:Prismcom有两个论点。第一个将永远是'usb'。之后发生的任何事情都会显示在标志上。

4 个答案:

答案 0 :(得分:20)

试试这个。将其复制到批处理文件(例如send.bat)中,然后只需运行send.bat即可将温度程序中的消息发送到prismcom程序。

temperature.exe > msg.txt
set /p msg= < msg.txt
prismcom.exe usb "%msg%"

答案 1 :(得分:12)

这应该有效:

for /F "tokens=*" %i in ('temperature') do prismcom.exe usb %i

如果在批处理文件中运行,则需要使用%%i而不是%i(在这两个地方)。

答案 2 :(得分:9)

您还可以使用PowerShell在Cmd.exe命令行上运行完全相同的命令。为简单起见,我会采用这种方法......

C:\>PowerShell -Command "temperature | prismcom.exe usb"

请阅读Understanding the Windows PowerShell Pipeline

您也可以在命令行输入C:\>PowerShell,它会立即让您进入PS C:\>模式,您可以直接开始编写PS。

答案 3 :(得分:0)

不确定是否要编写这些程序,但这只是一个简单的示例。

program1.c

#include <stdio.h>
int main (int argc, char * argv[] ) {
    printf("%s", argv[1]); 
    return 0;
}

rgx.cpp

#include <cstdio>
#include <regex>
#include <iostream>
using namespace std;
int main (int argc, char * argv[] ) {
    char input[200];
    fgets(input,200,stdin);
    string s(input)
    smatch m;
    string reg_exp(argv[1]);
    regex e(reg_exp);
    while (regex_search (s,m,e)) {
      for (auto x:m) cout << x << " ";
      cout << endl;
      s = m.suffix().str();
    }
    return 0;
}

同时编译两者,然后运行program1.exe "this subject has a submarine as a subsequence" | rgx.exe "\b(sub)([^ ]*)"

|操作符仅将program1的printf操作的输出从stdout重定向到stdin流,从而坐在那里等待rgx.exe接收

相关问题