Linux Shell中的I/O流重定向. Shell如何处理带有重定向的命令? [英] I/O stream redirection in a Linux shell. How does the shell process a command with redirection?

查看:100
本文介绍了Linux Shell中的I/O流重定向. Shell如何处理带有重定向的命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,我正在编写一个小的shell(重定向,管道,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

现在,我不知道以下情况下的过程(例如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 ^

解决方案

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

BASH从左到右处理IO重定向.

> cmd1 > file 2>&1
> cmd2 2>&1 > file

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

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

如果查看BASH的源代码,您会发现命令的执行分为几个步骤:

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

Currently I'm coding a small shell (redirection, pipes, exec, etc.). Been trying to figure out the steps the Linux shell takes in addressing I/O redirection.

Some questions on what I need help on:

  1. In which direction does the shell read from the command line when it is looking for the redirection? Left to right or the opposite? Uses recursion?

  2. What are the cases the shell needs to look for? (not sure if there are many or just a couple that can encompass a lot of variation)

Anyways, some I can think of are (correct me if I'm wrong):

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

Now I don't know the process on the following cases (as in how the shell looks for and handles these). The steps the shell takes are unbeknown to me

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 ^

解决方案

As long as you only redirect stdin and stdout, the order in which the redirections are processed don't matter so the last two examples are exactly the same.

BASH processes IO redirection from left to right.

> cmd1 > file 2>&1
> cmd2 2>&1 > file

These two are different. In the first case, I bind stdout to file and then bind stderr to stdout: Both stderr and stdout now go into the file.

In the second case, I bind (the child's) stderr to stdout (of the parent) and then I find the child's stdout to file. The result is that you now get the child's stderr output on stdout and the stdout goes to the file. This is useful for processing stderr in a pipe, for example.

If you look at the source code of BASH, you can see that the execution of a command is split in several steps:

  • Replace all variables
  • Split input into "words"
  • Process IO redirection (and remove the involved words)
  • Create a new child process with the correct IO setup and the remaining words as arguments.

这篇关于Linux Shell中的I/O流重定向. Shell如何处理带有重定向的命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆