使用Bash Scripting,如何抑制命令的所有输出?

时间:2021-12-03 15:43:11

I have a bash script that runs a program with parameters. That program outputs some status (doing this, doing that...). There is no option for this program to be quiet. How can I prevent the script from displaying anything?

我有一个运行带参数的程序的bash脚本。该程序输出一些状态(这样做,这样做......)。这个程序没有选择安静。如何防止脚本显示任何内容?

I am looking for something like windows "echo off".

我正在寻找类似windows“echo off”的东西。

7 个解决方案

#1


400  

The following sends standard output to the null device (bit bucket).

以下命令将标准输出发送到空设备(位桶)。

scriptname >/dev/null

and if you also want error messages to be sent there, use one of (the first may not work in all shells):

如果你还想在那里发送错误消息,请使用其中一个(第一个可能不适用于所有shell):

scriptname &>/dev/null
scriptname >/dev/null 2>&1
scriptname >/dev/null 2>/dev/null

and, if you want to record the messages but not see them, replace /dev/null with an actual file, such as:

并且,如果要记录消息但没有看到它们,请将/ dev / null替换为实际文件,例如:

scriptname &>scriptname.out

For completeness, under Windows cmd.exe (where "nul" is the equivalent of "/dev/null"), it is :

为了完整性,在Windows cmd.exe(其中“nul”相当于“/ dev / null”)下,它是:

scriptname >nul 2>nul

#2


32  

Something like

就像是

script > /dev/null 2>&1

This will prevent standard output and error output, redirecting them both to /dev/null.

这将阻止标准输出和错误输出,将它们重定向到/ dev / null。

#3


9  

Try

尝试

: $(yourcommand)

: is short for "do nothing".

:是“什么都不做”的缩写。

$() is just your command.

$()只是你的命令。

#4


5  

Like andynormancx post use this: (if you're working in an Unix environment)

像andynormancx一样使用这个:(如果你在Unix环境中工作)

scriptname > /dev/null

or you can use this: (if you're working in a Windows environment)

或者你可以使用它:(如果你在Windows环境中工作)

scriptname > nul

#5


4  

An alternative that may fit in some situations is to assign the result of a command to a variable:

在某些情况下可能适合的替代方法是将命令的结果分配给变量:

$ DUMMY=`grep root /etc/passwd`
$ echo $?
0
$ DUMMY=`grep r00t /etc/passwd`
$ echo $?
1

Since Bash and other POSIX commandline interpreters does not consider variable assignments as a command, the present command's return code is respected.

由于Bash和其他POSIX命令行解释器不将变量赋值视为命令,因此遵循当前命令的返回码。

#6


3  

Take a look at this example from The Linux Documentation Project:

从Linux文档项目看一下这个例子:

3.6 Sample: stderr and stdout 2 file

3.6示例:stderr和stdout 2文件

This will place every output of a program to a file. This is suitable sometimes for cron entries, if you want a command to pass in absolute silence.

这会将程序的每个输出都放在一个文件中。如果您希望命令以绝对静音方式传递,这有时适用于cron条目。

     rm -f $(find / -name core) &> /dev/null 

That said, you can use this simple redirection:

也就是说,你可以使用这个简单的重定向:

/path/to/command &>/dev/null

#7


1  

In you script you can add the following to the lines that you know are going to give an output:

在您的脚本中,您可以将以下内容添加到您知道要提供输出的行中:

some_code 2>>/dev/null

Or else you can also try

或者你也可以试试

some_code >>/dev/null

#1


400  

The following sends standard output to the null device (bit bucket).

以下命令将标准输出发送到空设备(位桶)。

scriptname >/dev/null

and if you also want error messages to be sent there, use one of (the first may not work in all shells):

如果你还想在那里发送错误消息,请使用其中一个(第一个可能不适用于所有shell):

scriptname &>/dev/null
scriptname >/dev/null 2>&1
scriptname >/dev/null 2>/dev/null

and, if you want to record the messages but not see them, replace /dev/null with an actual file, such as:

并且,如果要记录消息但没有看到它们,请将/ dev / null替换为实际文件,例如:

scriptname &>scriptname.out

For completeness, under Windows cmd.exe (where "nul" is the equivalent of "/dev/null"), it is :

为了完整性,在Windows cmd.exe(其中“nul”相当于“/ dev / null”)下,它是:

scriptname >nul 2>nul

#2


32  

Something like

就像是

script > /dev/null 2>&1

This will prevent standard output and error output, redirecting them both to /dev/null.

这将阻止标准输出和错误输出,将它们重定向到/ dev / null。

#3


9  

Try

尝试

: $(yourcommand)

: is short for "do nothing".

:是“什么都不做”的缩写。

$() is just your command.

$()只是你的命令。

#4


5  

Like andynormancx post use this: (if you're working in an Unix environment)

像andynormancx一样使用这个:(如果你在Unix环境中工作)

scriptname > /dev/null

or you can use this: (if you're working in a Windows environment)

或者你可以使用它:(如果你在Windows环境中工作)

scriptname > nul

#5


4  

An alternative that may fit in some situations is to assign the result of a command to a variable:

在某些情况下可能适合的替代方法是将命令的结果分配给变量:

$ DUMMY=`grep root /etc/passwd`
$ echo $?
0
$ DUMMY=`grep r00t /etc/passwd`
$ echo $?
1

Since Bash and other POSIX commandline interpreters does not consider variable assignments as a command, the present command's return code is respected.

由于Bash和其他POSIX命令行解释器不将变量赋值视为命令,因此遵循当前命令的返回码。

#6


3  

Take a look at this example from The Linux Documentation Project:

从Linux文档项目看一下这个例子:

3.6 Sample: stderr and stdout 2 file

3.6示例:stderr和stdout 2文件

This will place every output of a program to a file. This is suitable sometimes for cron entries, if you want a command to pass in absolute silence.

这会将程序的每个输出都放在一个文件中。如果您希望命令以绝对静音方式传递,这有时适用于cron条目。

     rm -f $(find / -name core) &> /dev/null 

That said, you can use this simple redirection:

也就是说,你可以使用这个简单的重定向:

/path/to/command &>/dev/null

#7


1  

In you script you can add the following to the lines that you know are going to give an output:

在您的脚本中,您可以将以下内容添加到您知道要提供输出的行中:

some_code 2>>/dev/null

Or else you can also try

或者你也可以试试

some_code >>/dev/null