Capture both stdout and stderr in Bash [duplicate]

There’s a really ugly way to capture stderr and stdout in two separate variables without temporary files (if you like plumbing), using process substitution, source, and declare appropriately. I’ll call your command banana. You can mimic such a command with a function: banana() { echo “banana to stdout” echo >&2 “banana to stderr” } I’ll … Read more

Bash script – store stderr in a variable [duplicate]

Try redirecting stderr to stdout and using $() to capture that. In other words: VAR=$((your-command-including-redirect) 2>&1) Since your command redirects stdout somewhere, it shouldn’t interfere with stderr. There might be a cleaner way to write it, but that should work. Edit: This really does work. I’ve tested it: #!/bin/bash BLAH=$(( ( echo out >&1 echo … Read more

How to tee to stderr?

The only cross platform method I found which works in both interactive and non-interactive shells is: command | tee >(cat 1>&2) The argument to tee is a file or file handle. Using process substitution we send the output to a process. In the process =cat=, we redirect stdout to stderr. The shell (bash/ksh) is responsible … Read more

redirect stdout/stderr to a string

Yes, you can redirect it to an std::stringstream: std::stringstream buffer; std::streambuf * old = std::cout.rdbuf(buffer.rdbuf()); std::cout << “Bla” << std::endl; std::string text = buffer.str(); // text will now contain “Bla\n” You can use a simple guard class to make sure the buffer is always reset: struct cout_redirect { cout_redirect( std::streambuf * new_buffer ) : old( … Read more

PHP StdErr after Exec()

If you want to execute a command, and get both stderr and stdout, not “merged”, a solution would probably to use proc_open, which provides a great level of control over the command that’s being executed — including a way to pipe stdin/stdout/stderr. And here is an example : let’s consider we have this shell-script, in … Read more

Temporarily Redirect stdout/stderr

You can also put the redirection logic in a contextmanager. import os import sys class RedirectStdStreams(object): def __init__(self, stdout=None, stderr=None): self._stdout = stdout or sys.stdout self._stderr = stderr or sys.stderr def __enter__(self): self.old_stdout, self.old_stderr = sys.stdout, sys.stderr self.old_stdout.flush(); self.old_stderr.flush() sys.stdout, sys.stderr = self._stdout, self._stderr def __exit__(self, exc_type, exc_value, traceback): self._stdout.flush(); self._stderr.flush() sys.stdout = self.old_stdout sys.stderr … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)