文件描述符如何工作? [英] How do file descriptors work?

查看:77
本文介绍了文件描述符如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我为什么这行不通吗?我在玩文件描述符,但是有点迷路.

Can someone tell me why this does not work? I'm playing around with file descriptors, but feel a little lost.

#!/bin/bash
echo "This"
echo "is" >&2
echo "a" >&3
echo "test." >&4

前三行运行正常,但后两行出错.为什么?

The first three lines run fine, but the last two error out. Why?

推荐答案

文件描述符0、1和2分别用于stdin,stdout和stderr.

File descriptors 0, 1 and 2 are for stdin, stdout and stderr respectively.

文件描述符3、4,.. 9用于附加文件.为了使用它们,您需要先打开它们.例如:

File descriptors 3, 4, .. 9 are for additional files. In order to use them, you need to open them first. For example:

exec 3<> /tmp/foo  #open fd 3.
echo "test" >&3
exec 3>&- #close fd 3.

有关更多信息,请参见高级Bash脚本指南:第20章.I/O重定向.

For more information take a look at Advanced Bash-Scripting Guide: Chapter 20. I/O Redirection.

这篇关于文件描述符如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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