文件描述符 [英] File descriptors

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

问题描述

有人能告诉我为什么不起作用?我玩弄文件描述符,反而觉得有点失落。

Can some one 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? Thanks for the help!

推荐答案

文件描述符0,1和2分别是标准输入,输出和错误。

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天全站免登陆