zsh如何处理多重重定向?

huangapple go评论144阅读模式
英文:

How can zsh handle multiple redirections?

问题

例如,在zsh中,你可以这样做:

% cat < file1 < file2

然后你会看到两者的内容。zsh是如何在对cat程序透明的情况下做到这一点的?

cat程序在POSIX系统上从STDIN_FILENO读取并打印到STDOUT_FILENO,那么一个文件描述符如何指向多个打开的文件描述符?在sh中,只有最后的重定向(最右边的)会执行。

英文:

For example, in zsh, you can do:

% cat &lt; file1 &lt; file2

and you will see both. How can zsh do this while being transparent to the cat program?

The cat program reads from STDIN_FILENO and prints to STDOUT_FILENO on a POSIX system, so how can that one file descriptor point to multiple open file descriptions? In sh, only the last redirection (rightmost) is performed.

答案1

得分: 5

"zsh"将其称为"multios",输入重定向通过使用子进程按顺序读取文件并将其内容转发到重定向到的进程的标准输入的管道来实现。

来自文档

如果用户尝试多次打开用于读取的文件描述符,则shell将文件描述符打开为管道,以将所有指定的输入按照指定的顺序复制到其输出中,前提是设置了MULTIOS选项。

(对于相同描述符的多次输出重定向采用类似的方法。)

因此,something < file1 < file2大致等同于cat file1 file2 | something,尽管关于何时打开输入文件存在一些细微差别 - 一次性打开还是与cat一样按顺序打开。

英文:

zsh calls this "multios", and input redirection works by using a child process to read the files in order and forward their contents to a pipe connected to the standard input of the process being redirected to.

From the documentation:

>If the user tries to open a file descriptor for reading more than once, the shell opens the file descriptor as a pipe to a process that copies all the specified inputs to its output in the order specified, provided the MULTIOS option is set.

(A similar approach is taken with multiple output redirections of the same descriptor.)

So something &lt;file1 &lt;file2 is roughly equivalent to cat file1 file2 | something, though there are some subtle differences about when the input files are opened - all at once vs sequentially with cat.

答案2

得分: 3

The program is provided a handle to a pipe, rather than a handle to a plain file. zsh reads the files and writes them to the pipe.

In other words,

prog <file1 <file2

results in something similar to

cat file1 file2 | prog

Differences:

  • It doesn't actually use cat, but it does create a new process to assist it.
  • All the file handles are opened up front rather than when needed.
英文:

The program is provided a handle to a pipe, rather than a handle to a plain file.<sup>[1]</sup> zsh reads the files and writes them to the pipe.

In other words,

prog &lt;file1 &lt;file2

results in something similar to

cat file1 file2 | prog

Differences:

  • It doesn't actually use cat, but it does create a new process to assist it.<sup>[2]</sup>
  • All the file handles are opened up front rather than when needed.

  1. &#x20;

    % perl -e&#39;exec &quot;ls&quot;, &quot;-l&quot;, &quot;/proc/$$/fd&quot;&#39; &lt;file1 &lt;file2
    total 0
    lr-x------ 1 ikegami ikegami 64 Jun  4 16:16 0 -&gt; &#39;pipe:[26665]&#39;
    lrwx------ 1 ikegami ikegami 64 Jun  4 16:16 1 -&gt; /dev/pts/0
    lrwx------ 1 ikegami ikegami 64 Jun  4 16:16 2 -&gt; /dev/pts/0
    lr-x------ 1 ikegami ikegami 64 Jun  4 16:16 3 -&gt; /proc/366/fd
    
  2. &#x20;

    % ps
      PID TTY          TIME CMD
       15 pts/0    00:00:00 bash
      338 pts/0    00:00:00 zsh
      372 pts/0    00:00:00 ps
    
    % perl -e&#39;exec &quot;ps&quot;&#39; &lt;file1 &lt;file2
      PID TTY          TIME CMD
       15 pts/0    00:00:00 bash
      338 pts/0    00:00:00 zsh
      373 pts/0    00:00:00 ps
      374 pts/0    00:00:00 zsh &lt;defunct&gt;    &lt;- Helper
    

huangapple
  • 本文由 发表于 2023年6月5日 02:19:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定