bash 重定向到 /dev/null: 没有那个文件或目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15361482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:49:33  来源:igfitidea点击:

redirect to /dev/null: No such file or directory

bash

提问by Sallyerik

I would like to know if somebody could help me with this error:

我想知道是否有人可以帮助我解决此错误:

wc: Files/Unused-CMA_host.txt: No such file or directory

wc: Files/Unused-CMA_host.txt: 没有那个文件或目录

The file doesn't exist, then I want to redirect the output to /dev/null

该文件不存在,然后我想将输出重定向到 /dev/null

I try with this sentence > /dev/null 2>&1which is working in other case, but not here:

我尝试使用这句话> /dev/null 2>&1,它在其他情况下有效,但不是在这里:

wc -l Files/Unused-CMA_host.txt | awk '{print $1}' > /dev/null 2>&1

wc -l Files/Unused-CMA_host.txt | awk '{print $1}' > /dev/null 2>&1

somebody know why?

有人知道为什么吗?

thanks.

谢谢。

回答by chepner

Redirections apply to individual components of a pipeline, not a pipeline as a whole. In your example, you only redirect awk's standard output. To redirect the standard error and stardard output of the entire pipeline would require a command group such as

重定向适用于管道的各个组件,而不是整个管道。在您的示例中,您只重定向awk的标准输出。要重定向整个管道的标准错误和标准输出将需要一个命令组,例如

{ wc -l Files/Unused-CMA_host.txt | awk '{print }' ; } > /dev/null 2>&1

However, if the file doesn't exist, there won't be any standard output. Perhaps you want to redirect standard error to suppress the error message? Then you can simply use

但是,如果该文件不存在,则不会有任何标准输出。也许您想重定向标准错误以抑制错误消息?然后你可以简单地使用

wc -l Files/Unused-CMA_host.txt 2> /dev/null | awk '{print }'

In the case of a non-existent file, awkwill simply read from an empty stream and do nothing.

在文件不存在的情况下,awk将简单地从空流中读取并且什么都不做。