Linux Shell中的I / O流重定向。Shell如何处理带有重定向的命令?

Joh*_*ohn 4 linux bash io-redirection dup

目前,我正在编写一个小的外壳程序(重定向,管道,exec等)。一直在尝试弄清楚Linux Shell在解决I / O重定向方面采取的步骤。

关于我需要帮助的一些问题:

  1. 寻找重定向时,shell从命令行读取哪个方向?从左到右还是相反?使用递归?

  2. 外壳需要寻找什么情况?(不确定是否有很多或只有一对可以包含很多变化)

无论如何,我能想到的是一些(如果我错了,请纠正我):

cmd > file1       # stdout of cmd goes to file

cmd file1 > file2 # stdout of cmd with file1 as an argument goes to file2

cmd file2 < file1 # stdin on file2 comes from file1
Run Code Online (Sandbox Code Playgroud)

现在,我不知道在以下情况下的过程(如外壳如何查找和处理这些情况)。Shell所采取的步骤对我来说是未知的

cmd file2 > file3  < file1 # using "tee" in place of "cmd" I don't know
                           # how to do the dups and when to exec

cmd file2 < file3 > file1  # same ^
Run Code Online (Sandbox Code Playgroud)

Aar*_*lla 5

只要您仅重定向stdin和stdout,重定向的处理顺序就无关紧要,因此最后两个示例完全相同。

BASH处理从左到右的IO重定向。

> cmd1 > file 2>&1
> cmd2 2>&1 > file
Run Code Online (Sandbox Code Playgroud)

这两个是不同的。在第一种情况下,我将stdout file绑定到标准输出,然后将stderr绑定到stdout:现在,stderr和stdout都进入文件了。

在第二种情况下,我将(孩子的)stderr绑定到(父母的)stdout,然后找到要归档的孩子的stdout。结果是您现在在stdout上获得了孩子的stderr输出,并且stdout进入了文件。例如,这对于在管道中处理stderr很有用。

如果查看BASH的源代码,可以看到命令的执行分为几个步骤:

  • 替换所有变量
  • 将输入分为“单词”
  • 处理IO重定向(并删除涉及的单词)
  • 使用正确的IO设置以及其余单词作为参数创建一个新的子进程。