如何使用Bash重定向标准输入和输出 [英] How to redirect standard input and output with Bash

查看:132
本文介绍了如何使用Bash重定向标准输入和输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#!/bin/bash
./program < input.txt > output.txt

> output.txt 部分被忽略,因此output.txt最终为空。

The > output.txt part is being ignored so output.txt ends up being empty.

这适用于 sort 命令所以我希望也适用于其他程序。

This works for the sort command so I expected to also work for other programs.

这不起作用的任何原因?我该怎么做?

Any reason this doesn't work? How should I achieve this?

推荐答案

最可能的解释是你看到的输出来自 stderr ,而不是 stdout 。要将它们重定向到文件,请执行以下操作:

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:

./program < input.txt > output.txt 2>&1

./program < input.txt &> output.txt

这篇关于如何使用Bash重定向标准输入和输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆