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

前端 未结 4 529
生来不讨喜
生来不讨喜 2020-12-03 02:26

I have a program which sends text to an LED sign.

prismcom.exe

To use the program to send \"Hello\":

prismcom.exe usb Hello

相关标签:
4条回答
  • 2020-12-03 02:59

    Not sure if you are coding these programs, but this is a simple example of how you'd do it.

    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;
    }
    

    Compile both then run program1.exe "this subject has a submarine as a subsequence" | rgx.exe "\b(sub)([^ ]*)"

    The | operator simply redirects the output of program1's printf operation from the stdout stream to the stdin stream whereby it's sitting there waiting for rgx.exe to pick up.

    0 讨论(0)
  • 2020-12-03 03:11

    You can also run exactly same command at Cmd.exe command-line using PowerShell. I'd go with this approach for simplicity...

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

    Please read up on Understanding the Windows PowerShell Pipeline

    You can also type in C:\>PowerShell at the command-line and it'll put you in PS C:\> mode instanctly, where you can directly start writing PS.

    0 讨论(0)
  • 2020-12-03 03:15

    Try this. Copy this into a batch file - such as send.bat - and then simply run send.bat to send the message from the temperature program to the prismcom program.

    temperature.exe > msg.txt
    set /p msg= < msg.txt
    prismcom.exe usb "%msg%"
    
    0 讨论(0)
  • 2020-12-03 03:20

    This should work:

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

    If running in a batch file, you need to use %%i instead of just %i (in both places).

    0 讨论(0)
提交回复
热议问题