如何使用 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:59:22  来源:igfitidea点击:

How to redirect standard input and output with Bash

bashio

提问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