stderr

How to execute a command and get return code stdout and stderr of command in C++

怎甘沉沦 提交于 2019-11-30 09:58:30
Given the following answer (first c++11 answer): How to execute a command and get output of command within C++ using POSIX? Here is the implementation for your convinience: #include <cstdio> #include <iostream> #include <memory> #include <stdexcept> #include <string> #include <array> std::string exec(const char* cmd) { std::array<char, 128> buffer; std::string result; std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose); if (!pipe) throw std::runtime_error("popen() failed!"); while (!feof(pipe.get())) { if (fgets(buffer.data(), 128, pipe.get()) != nullptr) result += buffer.data(); } return

shell重定向的顺序问题

旧街凉风 提交于 2019-11-30 05:27:58
三个默认的文件描述符 0 : stdin(标准输入) 1 : stdout(标准输出) 2 : stderr(标准错误输出) 系统中这3个文件描述符所对应的文件: 重定向顺序 示例脚本 echo "hello world" echo "xxx" sh test.sh hello world xxx sh test.sh >/tmp/out $ cat /tmp/out hello world xxx sh -x test.sh >/tmp/out 2>/tmp/err stdout和stderr分别被重定向到/tmp/out和/tmp/err。 $ cat /tmp/out hello world xxx $ cat /tmp/err + echo 'hello world' + echo xxx sh -x test.sh 2>&1 >/tmp/out stdout被重定向到/tmp/out,stderr被重定向到stdout。因为在执行2>&1的时候,stdout对应的文件为/dev/pts/2,所以stderr的输出仍为stdout,后面的stdout被重定向到/tmp/out。 $ sh -x test.sh 2>&1 >/tmp/out + echo 'hello world' + echo xxx $ cat /tmp/out hello world xxx 文件描述符

How to Pipe Output to a File When Running as a Systemd Service?

試著忘記壹切 提交于 2019-11-30 04:18:37
I'm having trouble piping the STDOUT & STDERR to a file when running a program as a systemd service. I've tried adding the following to the .service file: ExecStart=/apppath/appname > /filepath/filename 2>&1 But this doesn't work. The output is ending up in /var/log/messages and is viewable using journalctl but I'd like a separate file. I've also tried setting StdOutput=tty but can't find a way of redirecting this to a file. Any help would be appreciated. systemd.service(5) says: ExecStart= Commands with their arguments that are executed when this service is started. So, systemd runs your

Displaying errors with sweave

你。 提交于 2019-11-30 03:38:38
问题 I'm writing some R notes with Sweave and would like to show common errors. For example, <<echo=TRUE, eval=TRUE>>= x = 5 #Case matters! x*X @ However when sweaving, the document won't compile due to the R error. Is there any way to make sweave compile and show the (nicely formated) error? 回答1: As Shane suggests, use <<echo=TRUE,eval=FALSE>> for the code that will error, but you want to display, and then again with <<echo=FALSE,eval=TRUE,results=verbatim>> but with the same code wrapped in a

How can I kill a process when a specific string is seen on standard error?

给你一囗甜甜゛ 提交于 2019-11-30 03:13:28
问题 I need to start a process, lets say foo . I would like to see the stdout/stderr as normal, but grep the stderr for string bar . Once bar is found in the stderr foo should be killed. Is this possible? 回答1: I initially wrote a way to do this that involved stream swizzling, but it wasn't very good. Some of the comments relate to that version. Check the history if you're curious. Here's a way to do this: (PIDFILE=$(mktemp /tmp/foo.XXXXXX) && trap "rm $PIDFILE" 0 \ && { foo \ 2> >(tee >(grep -q

Why does java.util.logging.Logger print to stderr?

流过昼夜 提交于 2019-11-30 02:32:51
问题 I've got a simple setup to log a message: JDK 8 Update 65 and Eclipse Mars import java.util.logging.Logger; public class Example { private final static Logger LOGGER = Logger.getLogger(Example.class.getName()); public static void main(String[] args) { LOGGER.info("Test"); } } I would expect to get an output on the stdout , just like using System.out.println(); . But instead it gets printed out on the stderr , which results in a red font on the eclipse console: I know that I can change this

Should I output warnings to STDERR or STDOUT?

柔情痞子 提交于 2019-11-30 00:02:03
I'm making a script that handles a predefined set of data, outputting to a file. I want to pop up a warning when one datum (which is always "Regular" in every set that I've had access to) is different stating that this value is unhandled (since I don't know how it affects the data). Should I output this warning to stderr or stdout? If I saved the output of this script (i.e. stdout only) so that I could process it later, would that warning interfere with how the output is parsed? Moreover, if output is piped to another process, the warning should show up on the terminal, so the user sees it

专题 6 目录文件编程

你说的曾经没有我的故事 提交于 2019-11-29 21:40:13
1. 对目录文件的操作包括创建、打开、定位和删除等内容, 其中 (1) 获取工作目录的函数有 getcwd 和 getwd 等。 (2) 设置工作目录的函数有 chdir 和 fchdir 等。 (3) 创建目录文件的函数有 mkdir 等。 (4) 删除目录文件的函数有 rmdir 等。 (5) 目录文件定位的函数有 seekdir 、 rewinddir 和 telldir 等。 2. 代码实例。 2.1 读取工作目录 ( 绝对路径 ) char *getcwd(char *buf, size_t size); char *getwd(char *pathname); /************ 实例 ***************/ #include <unistd.h> #include<stdio.h> int main() { char buf[255]; fprintf(stderr, “pwd = [%s]/n”, getcwd(buf, sizeof(buf)); chdir(“../”); fprintf(stderr, “pwd = [%s]/n”, getcwd(buf, sizeof(buf)); return 0; } 2.2 目录的创建与删除 int mkdir(const char *path, mode_t mode); int rmdir

bash: redirect (and append) stdout and stderr to file and terminal and get proper exit status

牧云@^-^@ 提交于 2019-11-29 21:21:30
To redirect (and append) stdout and stderr to a file, while also displaying it on the terminal, I do this: command 2>&1 | tee -a file.txt However, is there another way to do this such that I get an accurate value for the exit status? That is, if I test $? , I want to see the exit status of command , not the exit status of tee . I know that I can use ${PIPESTATUS[0]} here instead of $? , but I am looking for another solution that would not involve having to check PIPESTATUS . Perhaps you could put the exit value from PIPESTATUS into $? command 2>&1 | tee -a file.txt ; ( exit ${PIPESTATUS} )

Test stdout and stderr redirection in bash script

不羁岁月 提交于 2019-11-29 16:54:18
问题 I would like to test in my bash script where stdout and stderr are directed, or more precisely, if they have been redirected. Have you an idea ? The $* bash variable don't give me this info. 回答1: You should be able to use the -t test switch to tell if the output streams are tty s or not: if [ -t 1 ] ; then echo stdout is a terminal else echo stdout is not a terminal fi Use -t 0 for stdin . Use -t 2 for stderr . 回答2: Technically there is no way of telling whether stdin/stdout/stderr are