我该如何回应和控制台输出发送到一个文件中的一个蝙蝠脚本? [英] How do I echo and send console output to a file in a bat script?

查看:156
本文介绍了我该如何回应和控制台输出发送到一个文件中的一个蝙蝠脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个批处理脚本执行任务,并将输出发送到一个文本文件中。有没有办法让控制台窗口中的输出显示呢?

I have a batch script that executes a task and sends the output to a text file. Is there a way to have the output show on the console window as well?

例如:

c:\Windows>dir > windows-dir.txt

有没有办法让在控制台窗口 DIR 显示器的输出,以及把它放到文本文件?

Is there a way to have the output of dir display in the console window as well as put it into the text file?

推荐答案

我试图解释重定向了一下。

I try to explain the redirection a bit.

您重定向是>文件&LT十流;文件结果
它是不重要的,如果重定向之前或命令后,
所以这两条线几乎相同。

You redirect one of the ten streams with > file or < file
It is unimportant, if the redirection is before or after the command, so these two lines are nearly the same.

dir > file.txt
> file.txt dir

在这个例子中的重定向仅仅是一个快捷键 1> ,这意味着流1(标准输出)将被重定向。结果
所以,你可以使用重定向prepending一样数目的流 2> err.txt ,它也允许多个数据流在同一行重定向。

The redirection in this example is only a shortcut for 1>, this means the stream 1 (STDOUT) will be redirected.
So you can redirect any stream with prepending the number like 2> err.txt and it is also allowed to redirect multiple streams in one line.

dir 1> files.txt 2> err.txt 3> nothing.txt

在这个例子中,标准输出将进入files.txt,所有错误将是err.txt和STREAM3将进入nothing.txt(DIR不使用流3)。结果,
Stream0是STDIN结果
STREAM1是STDOUT结果
流2是STDERR结果
Stream3-9不使用

In this example the "standard output" will go into files.txt, all errors will be in err.txt and the stream3 will go into nothing.txt (DIR doesn't use the stream 3).
Stream0 is STDIN
Stream1 is STDOUT
Stream2 is STDERR
Stream3-9 are not used

但是,如果你尝试在同一数据流中多次重定向会发生什么?

But what happens if you try to redirect the same stream multiple times?

dir > files.txt > two.txt

只能有一,而且它始终是最后一个!结果
因此,它是等于 DIR> two.txt

好,有一个额外的可能性,重定向一个流至另一个流中。

Ok, there is one extra possibility, redirecting a stream to another stream.

dir 1>files.txt 2>&1 

2>&安培; 1 重定向到流2 STREAM1和 1> files.txt 重定向所有的 files.txt 结果
顺序很重要,在这里!

2>&1 redirects stream2 to stream1 and 1>files.txt redirects all to files.txt.
The order is important here!

dir ... 1>nul 2>&1
dir ... 2>&1 1>nul

是不同的。第一个重定向所有(stdout和stderr)到NUL,结果
但第二行重定向STDOUT到NUL和STDERR到空标准输出。

are different. The first one redirects all (STDOUT and STDERR) to NUL,
but the second line redirects the STDOUT to NUL and STDERR to the "empty" STDOUT.

作为一个结论,那是显而易见的,为什么OtávioDécio和andynormancx的例子不能工作

As one conclusion, it is obvious why the examples of Otávio Décio and andynormancx can't work.

command > file >&1
dir > file.txt >&2

都试图重定向流1两次,但只能有一,而且它总是最后一个。结果
所以,你得到

Both try to redirect stream1 two times, but "There can be only one", and it's always the last one.
So you get

command 1>&1
dir 1>&2

和在流1到流1的第一样本改向不允许(而不是非常有用)。

And in the first sample redirecting of stream1 to stream1 is not allowed (and not very useful).

希望它有助于结果
杰布

hope it helps
jeb

这篇关于我该如何回应和控制台输出发送到一个文件中的一个蝙蝠脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆