2014-12-24 41 views
0

我有点困惑,为什么当我将echo命令的标准输出重定向到标准错误时,为什么仍然会在终端打印参数?回声的标准输出重定向到标准错误,仍然在终端产生输出?

这里的是我跑

echo "potato" >&2 

可能有人给我讲解一下?如果输出重定向到其他地方,该命令如何输出任何内容?

谢谢:)

+0

什么'回声“马铃薯”&>/dev/null'给你? – MeetTitan

+0

它没有给我任何输出,但这对我来说是有意义的,如果我仍然从中得到输出,我会感到困惑,这在我的问题中看起来是类似的情况。 –

+0

你的意思是为什么'&> 2'和'>&2'不同?这是因为'&2'被视为文件描述符。 – BroSlow

回答

0

那么,默认情况下,您的终端显示STDOUT和STDERR。

所以,你看到的是STDERR。

如果你想隐藏STDERR:echo "potato" 2>/dev/null >&2

/dev/null是一个黑洞,在那里你可以重定向的东西,你不希望看到:)

0

由于标准误差也默认显示在终端。因此,您将标准输出重定向到标准错误,然后将其重定向到控制台。结果不会改变。

1

我想你想要的是:

bash-3.2$ echo "potato" &>2 
bash-3.2$ 

man页对于bash:

Redirecting Standard Output and Standard Error 
     Bash allows both the standard output (file descriptor 1) and 
     the standard error output (file descriptor 2) to be redirected to the file 
     whose name is the expansion of word with this construct. 

     There are two formats for redirecting standard output and standard error: 

       &>word 
     and 
       >&word 

     Of the two forms, the first is preferred. This is semantically equivalent to 

       >word 2>&1 
+1

您正在重定向到一个名为'2'的文件 – BroSlow

1

起初,当你的终端和外壳开始,标准输出和STDERR指向终端输出。您的命令echo "potato" >&2要求将STDOUT重定向到STDERR指向的内容。因此这个命令完全没有效果。

下面是一些参考:

  • 文件描述符0(FD0)被命名为标准输入
  • 文件描述符1(FD1)被命名为STDOUT
  • 文件描述符2(FD2)被命名为STDERR
  • 文件描述符3(FD3)等没有特殊名称。
  • 任何unix程序最初都默认打开所有这3个文件描述符。
  • STDIN默认点到输入装置(终端打开程序的),通常最后到键盘
  • STDOUT,STDERR默认点到控制台(终端)输出
  • > somefile1> somefile重定向文件描述符1命名为'somefile'的文件,即将STDOUT指向'somefile'
  • n> somefile将文件描述符n重定向到名为'somefile'的文件,其中n = 1,2,3,... n默认为1,当n被省略。
  • n>&m将文件描述符n重定向到文件描述符m。
  • 当我们说文件描述符n被重定向到文件描述符m时,我们实际上是指文件描述符n被强制指向哪个文件描述符m指向例如终端输出设备/控制台。
  • n>&-关闭文件描述符n,其中n = 1,2,3,...
  • 重定向的顺序很重要。它们从左到右应用。见下面的例子:

# FD1 (file descriptor1, i.e. STDOUT) is redirected (pointing) to the file named 'file.log` 
# Then FD2 is pointing to what FD1 points to, i.e. 'file.log'. Thus the following command 
# redirection both "potato" and the error output of 'ls xx' to file.log: 

$ (echo "potato"; ls xx) > file.log 2>&1 
$ cat file.log 
potato 
ls: cannot access xx: No such file or directory 

# FD2 is redirected (pointing) to what FD1 points to, i.e. the terminal output. (This has no effect, since FD2 was pointed to the terminal anyway. 
# FD1 is then redirected to file.log 
# Thus the following command only redirects "potato" to file.log, and left the error message 
# displayed on the terminal. 

$ (echo "potato"; ls xx) 2>&1 > file.log 
ls: cannot access xx: No such file or directory 
$ cat file.log 
potato 

  • 订货重定向和管之间(第一管,然后重定向)。因此,该命令:command1 > /dev/null | comamnd2首先在命令1和命令2之间创建管道,即命令1的链接STDOUT与命令2的STDIN之间的管道。然后,command1的STDOUT被重定向到/ dev/null。这基本上取消了管道(分离管道)。因此command2将看到STDIN输入的结束,即command2的STDIN被关闭。

所以,这解释了为什么下面的命令交换STDIN和STDOUT:

$ (echo xx; ls xx) 3>&1 1>&2 2>&3 3>&- | wc -l 
ls: cannot access xx: No such file or directory 
1 
  • 第一步:创建管道,FD1(左侧)指向管了。到管中
  • 第二步的wc点FD0:FD3被创建,复制FD1,即指向管出
  • 步骤3:FD1现在改变为什么FD2指向,即端子输出。
  • 步骤4:现在将FD2更改为FD3指向的内容,即管道输出。
  • 第五步:FD3关闭。

净效应为: FD1现在指向终端输出。 FD2现在指向管道输出,管道输出为wc命令。

希望这会有所帮助。

0

输出只需要去的地方它叫去

默认文件描述符1和2指向同一个位置(注意:>&2相当于1>&2

> $ ls -l /proc/$$/fd/ 
total 0 
lrwx------ 1 foo foo 64 Dec 23 18:42 0 -> /dev/pts/3 
lrwx------ 1 foo foo 64 Dec 23 18:42 1 -> /dev/pts/3 
lrwx------ 1 foo foo 64 Dec 23 18:42 2 -> /dev/pts/3 

现在假设我们重定向的文件描述符的一个指向别处

> exec 1>foo 
> ls -l /proc/$$/fd/ 
> exec 1>&2 
> cat foo 
total 0 
lrwx------ 1 foo foo 64 Dec 23 18:42 0 -> /dev/pts/3 
lrwx------ 1 foo foo 64 Dec 23 18:42 1 -> /home/foo/foo 
lrwx------ 1 foo foo 64 Dec 23 18:42 2 -> /dev/pts/3  

ls -l /proc/$$/fd/输出我们nt在我们的工作目录中将foo文件存档,而不是打印到标准输出。

相关问题