简体   繁体   English

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

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

I have a program which sends text to an LED sign. 我有一个程序将文本发送到LED标志。

prismcom.exe prismcom.exe

To use the program to send "Hello": 要使用该程序发送“Hello”:

prismcom.exe usb Hello

Now, I wish to, for example use a command program called Temperature. 现在,我希望使用一个名为Temperature的命令程序。

temperature

Let's say the program gives your computer's temperature. 假设程序给出了计算机的温度。

Your computer is 100 degrees Fahrenheit.

Now, I wish to write the output of temperature to prismcom.exe: 现在,我希望将温度输出写入prismcom.exe:

temperature | prismcom.exe usb

This does not seem to work. 这似乎不起作用。

Yes, I've looked for a solution to this for more than twenty minutes. 是的,我已经找了20多分钟的解决方案了。 In all cases, they are either kludges/hacks or a solution for something besides the Windows command line. 在所有情况下,它们都是kludges / hacks或Windows命令行之外的解决方案。

I would appreciate direction as to how I would pipe the output from temperature to prismcom. 我很欣赏如何将输出从温度传输到prismcom的方向。

Thanks! 谢谢!

Edit: Prismcom has two arguments. 编辑:Prismcom有两个论点。 The first will always be 'usb'. 第一个将永远是'usb'。 Anything that comes after that will be displayed on the sign. 之后发生的任何事情都会显示在标志上。

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. 将其复制到批处理文件(例如send.bat)中,然后运行send.bat将消息从温度程序发送到prismcom程序。

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

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). 如果在批处理文件中运行,则需要使用%%i而不仅仅是%i (在两个位置)。

You can also run exactly same command at Cmd.exe command-line using PowerShell. 您还可以使用PowerShell在Cmd.exe命令行上运行完全相同的命令。 I'd go with this approach for simplicity... 为简单起见,我会采用这种方法......

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

Please read up on Understanding the Windows PowerShell Pipeline 请阅读了解Windows PowerShell管道

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. 您也可以在命令行输入C:\\>PowerShell ,它会立即进入PS C:\\>模式,您可以直接开始编写PS。

Not sure if you are coding these programs, but this is a simple example of how you'd do it. 不确定您是否正在编写这些程序,但这是一个如何执行此操作的简单示例。

program1.c program1.c

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

rgx.cpp 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)([^ ]*)" 然后编译两个然后运行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)([^ ]*)"

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. operator只是简单地将program1的printf操作的stdoutstdout流重定向到stdin流,然后它就坐在那里等待rgx.exe接收。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在本机窗口中运行两个命令提示符终端,如何将一个输出重定向到另一个输入? - Running two command prompt terminals in native windows, how can I redirect the output of one to the input of the other? 在 Windows cmd 中,如何提示用户输入并在另一个命令中使用结果? - In Windows cmd, how do I prompt for user input and use the result in another command? 如何使用命令输出作为另一个命令的参数? - How To Use A Command Output As An Argument of Another Command? 我如何使用Shell命令的输出作为配方中的条件值 - how do I use the output of a shell command as a conditional value in a recipe 如何将上一个命令的输出的顶行解析为另一个命令的输入? - How can I parse the top line of output of a previous command as input to another command? 如何在命令中使用文件并将 output 重定向到同一个文件而不截断它(Windows)? - How can I use a file in a command and redirect output to the same file without truncating it (Windows)? 在Windows命令提示符下,如果没有输入,如何在一定时间内运行一个命令,如果没有输入,又如何运行? - In Windows Command prompt, how can I run one command if there is no input, and another if there is, within a certain time? 如何将输入从一个声卡重定向到另一个? - How to redirect input from one sound card to another? Go exec.Command pipe output 进入另一个 Z2F2D399F0EA88447359FE5514B3 - Go exec.Command pipe output into another powershell 如何解析getmac命令的output? - How do I parse the output of getmac command?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM