如何将shell输出捕获到文本文件?

Mik*_*ail 4 command-line windows-8

在 linux 中,我可以输入echo "hello" >& text.output并在文件中获取文本输出。

如何在 Windows 8 中获得这种输出?

有没有办法从输出中判断错误?

我的动机是尝试记录输出tracert并调查 Windows 8 和 Windows 7 之间的差异。

Ale*_*nov 6

与在 Linux 中的方式相同:

echo "Hello" >text.output
Run Code Online (Sandbox Code Playgroud)

错误通常打印到stderr流(它是#2)。您可以捕获stderr流:

del 1.txt 2>text.output
Run Code Online (Sandbox Code Playgroud)

如果文件1.txt不存在,text.output将包含Could not find 1.txt.


您可以重定向stdoutstderr

echo "Hello" 2>&1
Run Code Online (Sandbox Code Playgroud)

stderrstdout

echo "Hello" 1>&2
Run Code Online (Sandbox Code Playgroud)

PS 我不确定您的命令是否适用于 Linux。无论如何都会echo "hello" >& text.output产生语法错误:>& was unexpected at this time.

  • [鲜为人知的功能:重定向操作符可以在线上的任何地方。](http://stackoverflow.com/questions/7225630/how-to-echo-2-no-quotes-to-a-file-from-批处理脚本) (3认同)