英文:
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 < file1 < 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 <file1 <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 <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.<sup>[2]</sup> - All the file handles are opened up front rather than when needed.
-
 
% perl -e'exec "ls", "-l", "/proc/$$/fd"' <file1 <file2 total 0 lr-x------ 1 ikegami ikegami 64 Jun 4 16:16 0 -> 'pipe:[26665]' lrwx------ 1 ikegami ikegami 64 Jun 4 16:16 1 -> /dev/pts/0 lrwx------ 1 ikegami ikegami 64 Jun 4 16:16 2 -> /dev/pts/0 lr-x------ 1 ikegami ikegami 64 Jun 4 16:16 3 -> /proc/366/fd
-
 
% 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'exec "ps"' <file1 <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 <defunct> <- Helper
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论