我有点困惑,为什么当我将echo命令的标准输出重定向到标准错误时,为什么仍然会在终端打印参数?回声的标准输出重定向到标准错误,仍然在终端产生输出?
这里的是我跑
行echo "potato" >&2
可能有人给我讲解一下?如果输出重定向到其他地方,该命令如何输出任何内容?
谢谢:)
我有点困惑,为什么当我将echo命令的标准输出重定向到标准错误时,为什么仍然会在终端打印参数?回声的标准输出重定向到标准错误,仍然在终端产生输出?
这里的是我跑
行echo "potato" >&2
可能有人给我讲解一下?如果输出重定向到其他地方,该命令如何输出任何内容?
谢谢:)
那么,默认情况下,您的终端显示STDOUT和STDERR。
所以,你看到的是STDERR。
如果你想隐藏STDERR:echo "potato" 2>/dev/null >&2
/dev/null
是一个黑洞,在那里你可以重定向的东西,你不希望看到:)
由于标准误差也默认显示在终端。因此,您将标准输出重定向到标准错误,然后将其重定向到控制台。结果不会改变。
我想你想要的是:
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
您正在重定向到一个名为'2'的文件 – BroSlow
起初,当你的终端和外壳开始,标准输出和STDERR指向终端输出。您的命令echo "potato" >&2
要求将STDOUT重定向到STDERR指向的内容。因此这个命令完全没有效果。
下面是一些参考:
> somefile
或1> somefile
重定向文件描述符1命名为'somefile'的文件,即将STDOUT指向'somefile'n> somefile
将文件描述符n重定向到名为'somefile'的文件,其中n
= 1,2,3,... n
默认为1,当n
被省略。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
wc
点FD0:FD3被创建,复制FD1,即指向管出净效应为: FD1现在指向终端输出。 FD2现在指向管道输出,管道输出为wc
命令。
希望这会有所帮助。
输出只需要去的地方它叫去
默认文件描述符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
文件存档,而不是打印到标准输出。
什么'回声“马铃薯”&>/dev/null'给你? – MeetTitan
它没有给我任何输出,但这对我来说是有意义的,如果我仍然从中得到输出,我会感到困惑,这在我的问题中看起来是类似的情况。 –
你的意思是为什么'&> 2'和'>&2'不同?这是因为'&2'被视为文件描述符。 – BroSlow