windows 如何隐藏批处理输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2044882/
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-09 07:02:47  来源:igfitidea点击:

How to hide batch output

windowsbatch-file

提问by Pekka

I am putting the finishing touches to a batch script that transfers the contents of a locally edited web site to the internet.

我正在对一个批处理脚本进行最后润色,该脚本将本地编辑的网站的内容传输到 Internet。

The script opens in a console window, and outputs quite a lot of stuff the administrator needs to see in case something goes wrong. In that case, though, the output is being sent as E-Mail, so there is no need to display the output and confuse the user who runs the update unnecessarily. I need to display only a few lines (say, "starting synchronisation..." and "syncrhonisation complete").

该脚本在控制台窗口中打开,并输出管理员需要查看的大量内容,以防出现问题。但是,在这种情况下,输出将作为电子邮件发送,因此无需显示输出并让不必要地运行更新的用户感到困惑。我只需要显示几行(例如,“开始同步...”和“同步完成”)。

Can anybody think of a way to stop output in a batch script? Kind of a total "echo off"?

任何人都可以想出一种在批处理脚本中停止输出的方法吗?一种完全的“回声关闭”?

A simple

一个简单的

my_batch_file > nul

won't cut it, because as I said, a few things I need to show.

不会削减它,因为正如我所说,我需要展示一些东西。

回答by davr

Windows actually does have the notion of stdout and stderr. Here's an example:

Windows 实际上确实有 stdout 和 stderr 的概念。下面是一个例子:

test.bat:

测试.bat:

@echo off
echo verbose stuff 1
echo verbose stuff 2
echo verbose stuff 3
echo important stuff! >&2
echo verbose stuff 4

If you run it as 'test.bat' you'll get the full output. If you run it as 'test.bat >nul' then you'll only get the 'important' output (anything redirected to stderr, with the >&2)

如果您以“ test.bat”运行它,您将获得完整的输出。如果您将其作为 ' test.bat >nul'运行,那么您将只会获得“重要”输出(任何重定向到 stderr 的内容,带有 >&2)

回答by jspcal

your batch script could run itself

您的批处理脚本可以自行运行

@echo off
if "%1" == "1" goto else
echo start
call %0 1 > nul
echo done
goto done
:else:
echo do stuff
:done:

stdout of the second invocation goes to nul

第二次调用的标准输出变为 nul

回答by Anon.

You could write the few things that need showing to stderrinstead of stdout.

你可以写一些需要显示的东西stderr而不是stdout.

Or alternatively, redirect the outputs of the individual parts that you want hidden.

或者,重定向您想要隐藏的各个部分的输出。