c++ - stderr 和 stdout 的行为不同
问题描述
运行时 stdout 和 stderr 给出不同的tcsh
结果ksh
. 根据我的理解,ksh
在 tcsh 中的行为是正确的,但不是。我的目的是了解和之间的区别cout
,cerr
所以我写了一个小 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 中是相同的吗?
解决方案
根据此tcsh
手册页,您需要使用>>&
重定向错误输出(以及标准输出)
该手册还指出
shell 目前无法在不重定向标准输出的情况下重定向诊断输出,但
(command >> output-file) >& error-file
通常是一种可接受的解决方法。
总而言之,tcsh
实际上没有办法将错误输出仅重定向到文件。
推荐阅读
- sql - 按日期获取加入订单最新记录的记录
- java - 由于收到致命警报,无法在 NetBeans 中使用 Maven Web 项目创建新的 Java:protocol_version
- html - HTML 音频适用于 src,但不适用于
- android - Camera2 MediaRecorder - 如何以 WebM 格式录制?
- arrays - 声明中的数组长度可以是非常量吗?
- java - “无法从存储库创建 Maven 项目。”
- python - 如何让 pandas str.contains 更快
- azure-devops - Azure DevOps:排除对 yaml 管道文件的更新触发相同的管道?
- c++ - 我似乎无法弄清楚为什么我对文件的读/写功能不起作用
- c++ - 使用 ESP8266 上的 PubSubClient 将字节 * 转换为字符串