Redirecting Multiple stdins?

不羁的心 提交于 2019-12-07 14:31:51

问题


I have three files named One, Two, Three.

One contains:

1
3
2

Two contains:

4
6
5

Three contains:

7
9
8

When I give the following command:

$sort < One < Two < Three

I get the output:

7
8
9

But when I give the following command:

$sort One Two Three

I get the ouput:

1
2
3
4
5
6
7
8
9

Can anyone please shed light on what exaclty is happening here? Why does the input from 1 and 2 not taken into consideration in the first command?


回答1:


Your command is the same as:

sort 0<1 0<2 0<3

(file descriptor 0 is standard input)

Redirections are processed in the order they appear, from left to right.
sort command itself cannot see any of those files.
bash open file 1,2,3 at file descriptor 0 one by one.
So the right most one override left ones.
At last, sort read from file descriptor 0 which is bind to file 3.




回答2:


You can't redirect multiple files with bash. To work around this limitation you could use cat:

cat 1 2 3 | sort

On a side note, zsh supports what it calls mutlios:

 zsh$ setopt multios
 zsh$ sort < 1 < 2 < 3 > 4 > 5
 zsh$ tr '\n' ' ' < 4 < 5
 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 


来源:https://stackoverflow.com/questions/12371283/redirecting-multiple-stdins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!