首页 > 解决方案 > stderr 和 stdout 的行为不同

问题描述

运行时 stdout 和 stderr 给出不同的tcsh结果ksh. 根据我的理解,ksh在 tcsh 中的行为是正确的,但不是。我的目的是了解和之间的区别coutcerr所以我写了一个小 C++ 程序:

    std::cout<<"this goes in standard output\n";
    std::cerr<<"This goes in error output\n";

并从中生成out可执行文件。

我制作了两个 shell 脚本,一个用于 ksh,另一个用于 tcsh,如下所示:

:~/CPP/TEST [124]$ cat test
#!/bin/tcsh

      out 2>>error.log

:~/CPP/TEST [125]$ cat test1
#!/usr/bin/ksh

out 2>>error.log

:~/CPP/TEST [129]$ ./test
This goes in error output            //WRONG
:~/CPP/TEST [130]$ cat error.log
this goes in standard output         //WRONG
:~/CPP/TEST [131]$ ./test1
this goes in standard output         //OK
:~/CPP/TEST [132]$ cat error.log
this goes in standard output
This goes in error output            //OK

stderr,的行为不stdout应该在 shell 中是相同的吗?

标签: c++stdoutkshstderrtcsh

解决方案


根据tcsh手册页,您需要使用>>&重定向错误输出(以及标准输出)

该手册还指出

shell 目前无法在不重定向标准输出的情况下重定向诊断输出,但(command >> output-file) >& error-file 通常是一种可接受的解决方法。

总而言之,tcsh实际上没有办法将错误输出仅重定向到文件。


推荐阅读