有没有办法让Linux

有没有办法让Linux

本文介绍了有没有办法让Linux CLI IO重定向执着?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有多个管道的命令,如下所示:

I have multiple piped commands, like this:

find [options] | grep [options] | xargs grep [options]

他们每个人都可能产生错误(错误的权限,空格,在文件名错误等),我不感兴趣。所以,我想重定向所有错误到/ dev / null的。我知道我可以用 2>的/ dev / null的,每个命令。我可以设置IO重定向持续?理想情况下,我只是将它设置一次,在命令的开始/结束,然后它会影响所有后续/ preceding管道命令。此外,可IO重定向被永久设置,以便它继续影响到的所有命令,直到它被复位?

Each one of them can potentially produce errors (permissions errors, spaces-in-filenames errors, etc) that I am not interested in. So, I want to redirect all errors to /dev/null. I know I can do this with 2>/dev/null, for each command. Can I set IO redirection persistently? Ideally, I would just set it once, at the beginning/end of the command, and then it would affect all subsequent/preceding piped commands. Also, can IO redirection be set permanently, so that it continues to affect all commands until it is reset?

我使用bash(我检查了手册页bash的内置命令,并没有看到'>'和'<'字在上方,所以我认为这是一个linux的东西...对不起)

I'm using bash (I checked the man page for bash builtins and did not see the '>' and '<' characters at the top, so I assumed it was a linux thing... sorry)

推荐答案

我要假设你正在使用bash,或者至少是某种类似Bourne外壳。

I'm going to assume that you're using bash, or at least some sort of Bourne-like shell.

我也假设你希望避免的是以下内容:

I'm also assuming that what you want to avoid is the following:

find ... 2>/dev/null | grep ... 2>/dev/null | xargs ... 2>/dev/null

即。重复 2&gt;对管道中的每个环节的/ dev / null的部分

您可以做到这一点:

( find ... | grep ... | xargs ... ) 2>/dev/null

您还可以设置永久重定向如下:

You can also set the redirection permanently as follows:

exec 2>/dev/null

和(假设输出和错误进行了前两者指向同一个地方),你可以撤消与

and (assuming that STDOUT and STDERR were both pointing to the same place before), you can undo that with:

exec 2>&1

这篇关于有没有办法让Linux CLI IO重定向执着?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 05:26