English 中文(简体)
是否使用过“+0”(转往中层)?
原标题:are there uses for >&0 (redirect to stdin)?
  • 时间:2012-04-14 18:19:41
  •  标签:
  • bash
  • unix

你们可以做:

  • echo test >&1 (redirect to stdout, although it s already going there)
  • echo test >&2 (redirect to stderr)
  • echo test >&0 (redirect to stdin)

当我最后一个终点站仍然像其他两个楼一样印刷<条码>。 因此,首先,为什么这项工作根本上? 第二,是否很好地利用了转向 st?

最佳回答

更确切地说,<代码>>&0的确是重复档案描述0,作为档案记录 1. 如果方案大纲仅供阅读,那么当贵方案试图写上去做(文件说明1)时,就会出现错误,因为文件说明1也只供阅读。

You can demonstrate this by writing a small shell script that inspects its own file descriptors:

10156115.sh:

#!/bin/bash
bash -c  ls -l /proc/$$/fd  >&0

而后又以可识别的 st、 st和 st子援引:

$ touch stdin
$ ./10156115.sh < stdin > stdout 2> stderr

结果是,你在<代码>stderr上取得了以下成绩:

ls: write error: Bad file descriptor

然而,所有三个机构都是一个终点站:(简化产出)。

$ ls -l /proc/$$/fd
lrwx------ 0 -> /dev/pts/14
lrwx------ 1 -> /dev/pts/14
lrwx------ 2 -> /dev/pts/14
lrwx------ 255 -> /dev/pts/14

通常,这三条实际上都是开放式的读+器,因此,如果单单使用正常彩票,则<代码>>&0的中继器就没有任何效果。


是否有这方面的用途?

这里没有任何共同用途,但是,如果您的字母转头stdoutstderr<>/code>,则你可以将其用作ck的黑板,以打印到终端。

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

但我建议实际上这样做。

问题回答

出于历史原因,终端的标准文件编号为开放式读/正文,而不是读写(具体来说,它一度开放,二次开放<>。 这有时对希望提取管道投入的方案有用,但也对用户的投入(从<代码>stdout,或更常见的<代码>stderr)有用,尽管在这种情况下使用<代码>/dev/tty更为可靠。 某些系统仅对面值适用:*BSDs开放式袖珍(“管道”),有些系统公用事业(例如:<代码>ufsdump)取决于这一点。

转题tost 通常(即只允许书写)并不有用,因为大多数方案都期望可以读(或如上所示有时读/写)。

是的,<代码>>&0有很好的用途。 如果我想将投入和产出转用于同一装置(例如,序列港),我可以打开读写模式的 st门,并转而停下来:

rz <>/dev/ttyUSB0 >&0

不仅使我免于某些打字,而且如果我用文字做的话,我会感到快乐。





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Bash usage of vi or emacs

From a programming standpoint, when you set the bash shell to use vi or emacs via set -o vi or set -o emacs What is actually going on here? I ve been reading a book where it claims the bash shell ...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Perform OR on two hash outputs of sha1sum

I want perform sha1sum file1 and sha1sum file2 and perform bitwise OR operation with them using bash. Output should be printable i.e 53a23bc2e24d039 ... (160 bit) How can I do this? I know echo $(( ...

Set screen-title from shellscript

Is it possible to set the Screen Title using a shell script? I thought about something like sending the key commands ctrl+A shift-A Name enter I searched for about an hour on how to emulate ...

热门标签