簡體   English   中英

如何使用管道將一個命令的輸出重定向到另一個命令的輸入?

[英]How do I use a pipe to redirect the output of one command to the input of another?

我有一個程序將文本發送到LED標志。

prismcom.exe

要使用該程序發送“Hello”:

prismcom.exe usb Hello

現在,我希望使用一個名為Temperature的命令程序。

temperature

假設程序給出了計算機的溫度。

Your computer is 100 degrees Fahrenheit.

現在,我希望將溫度輸出寫入prismcom.exe:

temperature | prismcom.exe usb

這似乎不起作用。

是的,我已經找了20多分鍾的解決方案了。 在所有情況下,它們都是kludges / hacks或Windows命令行之外的解決方案。

我很欣賞如何將輸出從溫度傳輸到prismcom的方向。

謝謝!

編輯:Prismcom有兩個論點。 第一個將永遠是'usb'。 之后發生的任何事情都會顯示在標志上。

試試這個。 將其復制到批處理文件(例如send.bat)中,然后運行send.bat將消息從溫度程序發送到prismcom程序。

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

這應該工作:

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

如果在批處理文件中運行,則需要使用%%i而不僅僅是%i (在兩個位置)。

您還可以使用PowerShell在Cmd.exe命令行上運行完全相同的命令。 為簡單起見,我會采用這種方法......

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

請閱讀了解Windows PowerShell管道

您也可以在命令行輸入C:\\>PowerShell ,它會立即進入PS C:\\>模式,您可以直接開始編寫PS。

不確定您是否正在編寫這些程序,但這是一個如何執行此操作的簡單示例。

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.exe "this subject has a submarine as a subsequence" | rgx.exe "\\b(sub)([^ ]*)"

| operator只是簡單地將program1的printf操作的stdoutstdout流重定向到stdin流,然后它就坐在那里等待rgx.exe接收。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM