windows 如何使用管道将一个命令的输出重定向到另一个命令的输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14574170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I use a pipe to redirect the output of one command to the input of another?
提问by Austin Burk
I have a program which sends text to an LED sign.
我有一个程序可以将文本发送到 LED 标志。
prismcom.exe
棱镜com.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
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:
现在,我希望将温度的输出写入priscom.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. 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.
我很感激有关如何将输出从温度管道传输到棱镜通信的指导。
Thanks!
谢谢!
Edit: Prismcom has two arguments. The first will always be 'usb'. Anything that comes after that will be displayed on the sign.
编辑: Prismcom 有两个论点。第一个永远是'usb'。之后的任何内容都会显示在标志上。
采纳答案by Kenny Kerr
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%"
回答by Ostati
You can also run exactly same command at Cmd.exe command-line using PowerShell. I'd go with this approach for simplicity...
您还可以使用 PowerShell 在 Cmd.exe 命令行中运行完全相同的命令。为简单起见,我会采用这种方法......
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.
你也可以C:\>PowerShell
在命令行输入,它会立即让你进入PS C:\>
模式,在那里你可以直接开始写 PS。
回答by Harry Johnston
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
(在两个地方)。
回答by annoying_squid
Not sure if you are coding these programs, but this is a simple example of how you'd do it.
不确定您是否正在编写这些程序,但这是您如何编写的一个简单示例。
program1.c
程序1.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)([^ ]*)"
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.
该|
运算符只是将 program1 的printf
操作的输出从stdout
流重定向到stdin
流,从而它就在那里等待 rgx.exe 接收。