bash '>&0' 有没有用处(重定向到标准输入)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10156115/
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
are there uses for '>&0' (redirect to stdin)?
提问by Shep
In bash you can do:
在 bash 中,您可以执行以下操作:
echo test >&1(redirect to stdout, although it's already going there)echo test >&2(redirect to stderr)echo test >&0(redirect to stdin)
echo test >&1(重定向到标准输出,虽然它已经在那里了)echo test >&2(重定向到标准错误)echo test >&0(重定向到标准输入)
When I do the last one my terminal still prints testas it would with the other two, but it's hard to know why. So first off, why does this work at all? Secondly, are there any good uses for redirecting to stdin?
当我做最后一个时,我的终端仍然test像其他两个一样打印,但很难知道为什么。所以首先,为什么这会起作用?其次,重定向到标准输入有什么好的用途吗?
采纳答案by je4d
To be more precise, what >&0does is duplicate file descriptor 0 as file descriptor 1. If the program's stdin is only open for reading, then when your program tries to write to stdout (file descriptor 1), it will get an error because file descriptor 1 is also only open for reading.
更准确地说,什么>&0是将文件描述符 0 复制为文件描述符 1。如果程序的 stdin 仅打开供读取,那么当您的程序尝试写入 stdout(文件描述符 1)时,它会得到错误,因为文件描述符1 也只开放阅读。
You can demonstrate this by writing a small shell script that inspects its own file descriptors:
您可以通过编写一个检查自己的文件描述符的小 shell 脚本来演示这一点:
10156115.sh:
10156115.sh:
#!/bin/bash
bash -c 'ls -l /proc/$$/fd' >&0
And then invoke it with identifiable stdin, stdout and stderr:
然后使用可识别的 stdin、stdout 和 stderr 调用它:
$ touch stdin
$ ./10156115.sh < stdin > stdout 2> stderr
The result is that you get the following in stderr:
结果是你得到以下内容stderr:
ls: write error: Bad file descriptor
However, by default, all three are a terminal: (output simplified)
但是,默认情况下,所有三个都是终端:(输出简化)
$ ls -l /proc/$$/fd
lrwx------ 0 -> /dev/pts/14
lrwx------ 1 -> /dev/pts/14
lrwx------ 2 -> /dev/pts/14
lrwx------ 255 -> /dev/pts/14
Typically, all three are actually open read+write, so the >&0redirect has no effect at all if used alone from a normal shell.
通常,这三个实际上都是打开的读+写,因此>&0如果在普通 shell 中单独使用重定向,则根本没有任何影响。
Are there any uses for this?
这有什么用处吗?
There aren't any common uses of this, but you might use it as a dirty hack to get a way to print to the terminal if whomever calls your script redirects stdoutand stderr, and for whatever reason you're not able to change that:
这没有任何常见用途,但是如果有人调用您的脚本重定向stdout和stderr,并且无论出于何种原因您无法更改它,您都可以将其用作肮脏的黑客以获取打印到终端的方法:
if [ ! -t /dev/fd/1 -a ! -t /dev/fd/2 -a -t /dev/fd/0 ]; then
echo "My message that I really, really want to go to a terminal" >&0
fi
But I wouldn't recommend actually doing this.
但我不建议实际这样做。
回答by geekosaur
For historical reasons, the standard file descriptors on a terminal are open read/write instead of read-only (specifically, it's opened once and dup()ed to the others). This is occasionally useful for programs that want to take piped input but also read input from the user (from stdout, or more commonly stderr), although using /dev/ttyis more reliable in that case. Some systems apply this to more than just ttys: the *BSDs open socketpairs ("pipes") bidirectionally, and some system utilities (I recall ufsdumpbeing an example) depend on this.
由于历史原因,终端上的标准文件描述符是打开的读/写而不是只读的(具体来说,它被打开一次并被dup()其他人打开)。这对于想要获取管道输入但也从用户(来自stdout,或更常见的stderr)读取输入的程序有时很有用,尽管/dev/tty在这种情况下using更可靠。一些系统不仅仅将其应用于 tty:*BSD 双向打开套接字对(“管道”),并且一些系统实用程序(我记得ufsdump是一个例子)依赖于此。
Redirecting tostdin(that is, having it open only for write) is not generally useful, since most programs expect it to be open for read (or sometimes read/write, as above).
重定向到stdin(即,仅将其打开以进行写入)通常没有用,因为大多数程序都希望将其打开以进行读取(或有时为读取/写入,如上所述)。

