stderr

Piping both stdout and stderr in bash?

老子叫甜甜 提交于 2019-11-27 03:14:19
It seems that newer versions of bash have the &> operator, which (if I understand correctly), redirects both stdout and stderr to a file ( &>> appends to the file instead, as Adrian clarified). What's the simplest way to achieve the same thing, but instead piping to another command? For example, in this line: cmd-doesnt-respect-difference-between-stdout-and-stderr | grep -i SomeError I'd like the grep to match on content both in stdout and stderr (effectively, have them combined into one stream). Note : this question is asking about piping, not redirecting - so it is not a duplicate of the

Redirect subprocess stderr to stdout

亡梦爱人 提交于 2019-11-27 03:02:16
问题 I want to redirect the stderr output of a subprocess to stdout. The constant STDOUT should do that, shouldn't it? However, $ python >/dev/null -c 'import subprocess;\ subprocess.call(["ls", "/404"],stderr=subprocess.STDOUT)' does output something. Why is that the case, and how do I get the error message on stdout? 回答1: A close read of the source code gives the answer. In particular, the documentation is misleading when it says: subprocess.STDOUT Special value that (...) indicates that

Why does 'java -version' go to stderr?

扶醉桌前 提交于 2019-11-27 02:04:49
Is there any special reason for the results of java -version going to stderr ? For example, this command executed from Windows' prompt line: java -version > java_version.txt leaves the file java_version.txt empty. EDIT: The same happens with the help printed out after executing java.exe without any parameters. EDIT: Just out of a sheer curiosity I checked whether it has been always like that and it turned out it actually has. java -version goes to stderr in JDK 1.1.8 and also in JDK 1.2.2, however the outputs of java.exe without any parameters do not. Is there any special reason for the

launch an exe/process with stdin stdout and stderr?

旧城冷巷雨未停 提交于 2019-11-27 01:30:31
With C++ how do i launch an exe/process with stdin stdout and stderr? I know how to do this in .NET and i remember using popen in the past but popen seems to allow stdin OR stdout not both and not all 3. I need this for windows but a linux solution is welcome as i'll need it for the same project in the future. You shoud use CreateProcess from WinApi. It takes as argument an object of struct STARTUP_INFO type. You can set hStdin, hStdout, and hStderr fields of the object to redirect those streams of child process to file handles you want (file, pipe, socket...) MSalters A portable solution

Append text to stderr redirects in bash

本秂侑毒 提交于 2019-11-27 01:23:47
问题 Right now I'm using exec to redirect stderr to an error log with exec 2>> ${errorLog} The only downside is that I have to start each run with a timestamp since exec just pushes the text straight into the log file. Is there a way to redirect stderr but allow me to append text to it, such as a time stamp? 回答1: This is very interesting. I've asked a guy who knows bash quite well, and he told me this way: foo() { while IFS='' read -r line; do echo "$(date) $line" >> file.txt; done; }; First, that

Wrap subprocess' stdout/stderr

假装没事ソ 提交于 2019-11-26 22:59:21
问题 I'd like to both capture and display the output of a process that I invoke through Python's subprocess. I thought I could just pass my file-like object as named parameter stdout and stderr I can see that it accesses the fileno attribute - so it is doing something with the object. However, the write() method is never invoked. Is my approach completely off or am I just missing something? class Process(object): class StreamWrapper(object): def __init__(self, stream): self._stream = stream self.

Capture program stdout and stderr to separate variables

好久不见. 提交于 2019-11-26 22:43:28
Is it possible to redirect stdout from an external program to a variable and stderr from external programs to another variable in one run? For example: $global:ERRORS = @(); $global:PROGERR = @(); function test() { # Can we redirect errors to $PROGERR here, leaving stdout for $OUTPUT? $OUTPUT = (& myprogram.exe 'argv[0]', 'argv[1]'); if ( $OUTPUT | select-string -Pattern "foo" ) { # do stuff } else { $global:ERRORS += "test(): oh noes! 'foo' missing!"; } } test; if ( @($global:ERRORS).length -gt 0 ) { Write-Host "Script specific error occurred"; foreach ( $err in $global:ERRORS ) { $host.ui

How to redirect stderr in Python?

时光毁灭记忆、已成空白 提交于 2019-11-26 22:39:50
I would like to log all the output of a Python script. I tried: import sys log = [] class writer(object): def write(self, data): log.append(data) sys.stdout = writer() sys.stderr = writer() Now, if I "print 'something' " it gets logged. But if I make for instance some syntax error, say "print 'something# ", it wont get logged - it will go into the console instead. How do I capture also the errors from Python interpreter? I saw a possible solution here: http://www.velocityreviews.com/forums/showpost.php?p=1868822&postcount=3 but the second example logs into /dev/null - this is not what I want.

Shell redirection i/o order

妖精的绣舞 提交于 2019-11-26 22:16:47
I'm playing with i/o shell redirection. The commands I've tried (in bash): ls -al *.xyz 2>&1 1> files.lst and ls -al *.xyz 1> files.lst 2>&1 There is no any *.xyz file in current folder. These commands gives me the different results. The first command shows an error message ls: *.xyz: No such file or directory on the screen. But the second one prints this error message to the file. Why did the first command failed to write an err output to the file? This error: ls: *.xyz: No such file or directory is being written on stderr by ls binary. However in this command: ls -al *.xyz 2>&1 1> files.lst

How do I temporarily redirect stderr in Ruby?

可紊 提交于 2019-11-26 22:07:51
I'd like to temporarily redirect stderr in a Ruby script for the duration of a block, ensuring that I reset it to its original value at the end of the block. I had trouble finding how to do this in the ruby docs. In Ruby, $stderr refers to the output stream that is currently used as stderr, whereas STDERR is the default stderr stream. It is easy to temporarily assign a different output stream to $stderr . require "stringio" def capture_stderr # The output stream must be an IO-like object. In this case we capture it in # an in-memory IO object so we can return the string value. You can assign