unix 如何将stderr重定向到一个文件?

wwwo4jvm  于 2023-02-22  发布在  Unix
关注(0)|答案(1)|浏览(159)

在unix中,将标准错误描述符重定向到一个名为error.txt的文件需要什么命令?
到目前为止,我有以下命令:

find / -name "report*" ________ error.txt
n8ghc7c1

n8ghc7c11#

可以像这样使用stderr处理程序2

find / -name "report*" 2>error.txt

请参见示例:

$ ls a1 a2
ls: cannot access a2: No such file or directory  <--- this is stderr
a1                                               <--- this is stdout
$ ls a1 a2 2>error.txt
a1
$ cat error.txt 
ls: cannot access a2: No such file or directory  <--- just stderr was stored

BASH Shell: How To Redirect stderr To stdout ( redirect stderr to a File )中所示,这些是处理程序:
| 手柄|姓名|说明|
| - ------|- ------|- ------|
| 无|标准输入|标准输入(stdin)|
| 1个|标准输出|标准输出(stdout)|
| 第二章|标准错误|标准误差(stderr)|
请注意与&>error.txt的区别,&>error.txt同时重定向stdout和stderr(参见Redirect stderr and stdout in a bash scriptHow to redirect both stdout and stderr to a file):

$ ls a1 a2 &>error.txt
$ cat error.txt 
ls: cannot access a2: No such file or directory  <--- stdout and stderr
a1                                               <--- were stored

相关问题