如何使用 Bash 重定向标准输入和输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10076741/
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 to redirect standard input and output with Bash
提问by Inuart
#!/bin/bash
./program < input.txt > output.txt
The > output.txtpart is being ignored so output.txt ends up being empty.
该> output.txt部分被忽略,因此 output.txt 最终为空。
This works for the sortcommand so I expected to also work for other programs.
这适用于sort命令,所以我希望也适用于其他程序。
Any reason this doesn't work? How should I achieve this?
任何原因这不起作用?我应该如何实现这一目标?
回答by Oliver Charlesworth
The most likely explanation is that the output you're seeing is from stderr, not stdout. To redirect both of them to a file, do this:
最可能的解释是您看到的输出来自stderr,而不是stdout。要将它们都重定向到一个文件,请执行以下操作:
./program < input.txt > output.txt 2>&1
or
或者
./program < input.txt &> output.txt

