如何在 Bash 脚本中静音输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2292847/
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
How to silence output in a Bash script?
提问by Sam
I have a program that outputs to stdout and would like to silence that output in a Bash script while piping to a file.
我有一个输出到标准输出的程序,并希望在管道到文件时在 Bash 脚本中使该输出静音。
For example, running the program will output:
例如,运行程序会输出:
% myprogram
% WELCOME TO MY PROGRAM
% Done.
I want the following script to not output anything to the terminal:
我希望以下脚本不向终端输出任何内容:
#!/bin/bash
myprogram > sample.s
回答by John Kugelman
If it outputs to stderr as well you'll want to silence that. You can do that by redirecting file descriptor 2:
如果它也输出到 stderr,您将希望使其静音。您可以通过重定向文件描述符 2 来做到这一点:
# Send stdout to out.log, stderr to err.log
myprogram > out.log 2> err.log
# Send both stdout and stderr to out.log
myprogram &> out.log # New bash syntax
myprogram > out.log 2>&1 # Older sh syntax
# Log output, hide errors.
myprogram > out.log 2> /dev/null
回答by Debugger
Redirect stderr to stdout
将标准错误重定向到标准输出
This will redirect the stderr (which is descriptor 2) to the file descriptor 1 which is the the stdout.
这会将 stderr(即描述符 2)重定向到作为 stdout 的文件描述符 1。
2>&1
Redirect stdout to File
将标准输出重定向到文件
Now when perform this you are redirecting the stdout to the file sample.s
现在执行此操作时,您将标准输出重定向到文件 sample.s
myprogram > sample.s
Redirect stderr and stdout to File
将 stderr 和 stdout 重定向到 File
Combining the two commands will result in redirecting both stderr and stdout to sample.s
结合这两个命令将导致将 stderr 和 stdout 都重定向到 sample.s
myprogram > sample.s 2>&1
Redirect stderr and stdout to /dev/null
将 stderr 和 stdout 重定向到 /dev/null
Redirect to /dev/null
if you want to completely silent your application.
/dev/null
如果您想完全静音您的应用程序,请重定向到。
myprogram >/dev/null 2>&1
回答by Eduardo Cuomo
All output:
所有输出:
scriptname &>/dev/null
Portable:
便携的:
scriptname >/dev/null 2>&1
Portable:
便携的:
scriptname >/dev/null 2>/dev/null
For newer bash (no portable):
对于较新的 bash(不可移植):
scriptname &>-
回答by Matt
If you want STDOUT and STDERR both [everything], then the simplest way is:
如果你想要 STDOUT 和 STDERR 都[一切],那么最简单的方法是:
#!/bin/bash
myprogram >& sample.s
then run it like ./script
, and you will get no output to your terminal. :)
然后像 一样运行它./script
,你的终端不会有任何输出。:)
the ">&" means STDERR and STDOUT. the &
also works the same way with a pipe: ./script |& sed
that will send everything to sed
">&" 表示 STDERR 和 STDOUT。在&
还与管道相同的方式:./script |& sed
将一切发送到SED
回答by Henry H.
If you are still struggling to find an answer, specially if you produced a file for the output, and you prefer a clear alternative:
echo "hi" | grep "use this hack to hide the oputut :) "
如果您仍在努力寻找答案,特别是如果您为输出生成了一个文件,并且您更喜欢一个明确的替代方案:
echo "hi" | grep "use this hack to hide the oputut :) "
回答by Swathi
Try with:
尝试:
myprogram &>/dev/null
to get no output
没有输出
回答by Berni
Note: This answer is related to the question "How to turn off echo while executing a shell script Linux" which was in turn marked as duplicated to this one.
注意:此答案与问题“How to turn off echo while execution a shell script Linux”有关,该问题又被标记为与此问题重复。
To actually turn off the echo the command is:
要实际关闭回声,命令是:
stty -echo
stty-回声
(this is, for instance; when you want to enter a password and you don't want it to be readable. Remember to turn echo on at the end of your script, otherwise the person that runs your script won't see what he/she types in from then on. To turn echo on run:
(例如,当您想输入密码但又不希望密码可读时。请记住在脚本末尾打开 echo,否则运行脚本的人将看不到他的内容/she 从那时起输入。要打开回显,请运行:
stty echo
回声
回答by NVRM
Useful in scripts:
在脚本中有用:
Get only the STDERR in a file, while hiding any STDOUT even if the program to hide isn't existing at all(does not ever hang parent script), this alone was working:
仅获取文件中的 STDERR,同时隐藏任何 STDOUT,即使要隐藏的程序根本不存在(永远不会挂起父脚本),仅此一项即可:
stty -echo && ./programMightNotExist 2> errors.log && stty echo
Detach completely and silence everything, even killing the parent script won't abort ./prog
:
完全分离并使所有内容静音,即使杀死父脚本也不会中止./prog
:
./prog </dev/null >/dev/null 2>&1 &