stderr and command substitution

stderr and command substitution

am 18.09.2007 18:33:14 von Daniel Santos

Hi,

I want to run a command and assign any output to a variable. If the
command is successful it outputs to stdout. If not, it outputs to stderr.

Since I want to parse any of its output I need to redirect stderr to
stdout because from what I've read command substitution only outputs
to stdout.

redirecting the commands stderr to stdout with 2>&1 didn't work for me (I'm
using bash 3.1). I guess its because the command runs in a subshell.

Any ideas ?
Daniel Santos

Re: stderr and command substitution

am 18.09.2007 21:13:35 von pacman

In article <46effdca$0$26760$a729d347@news.telepac.pt>,
Daniel Santos wrote:
>Hi,
>
>I want to run a command and assign any output to a variable. If the
>command is successful it outputs to stdout. If not, it outputs to stderr.
>
>Since I want to parse any of its output I need to redirect stderr to
>stdout because from what I've read command substitution only outputs
>to stdout.

`cmd 2>&1` or $(cmd 2>&1) should work.

>
>redirecting the commands stderr to stdout with 2>&1 didn't work for me (I'm
>using bash 3.1). I guess its because the command runs in a subshell.

I doubt bash would have a bug this severe in a released version, but just in
case, I tested it:

pacman@kosh:~$ echo hello > tmpjunk ; x=`cat tmpjunk 2>&1` ; chmod 000 tmpjunk ; y=`cat tmpjunk 2>&1` ; rm -f tmpjunk ; echo x is /$x/ and y is /$y/
x is /hello/ and y is /cat: tmpjunk: Permission denied/
pacman@kosh:~$ echo $BASH_VERSION
3.1.17(1)-release

As you can see, the error stream from the second cat was correctly placed
into $y. I don't see anything wrong here.

--
Alan Curry
pacman@world.std.com

Re: stderr and command substitution

am 18.09.2007 21:15:29 von Ed Morton

Daniel Santos wrote:
> Hi,
>
> I want to run a command and assign any output to a variable. If the
> command is successful it outputs to stdout. If not, it outputs to stderr.
>
> Since I want to parse any of its output I need to redirect stderr to
> stdout because from what I've read command substitution only outputs
> to stdout.
>
> redirecting the commands stderr to stdout with 2>&1 didn't work for me (I'm
> using bash 3.1). I guess its because the command runs in a subshell.
>
> Any ideas ?
> Daniel Santos

Shows us what happens when you do this:

$ ls garbage
ls: garbage: No such file or directory
$ foo=`ls garbage`
ls: garbage: No such file or directory
$ echo "$foo"

$ foo=`ls garbage 2>&1`
$ echo "$foo"
ls: garbage: No such file or directory
$

and what happens when you replaces "ls garbage" with your command.

Regards,

Ed.

Re: stderr and command substitution

am 19.09.2007 13:08:42 von Daniel Santos

On Tue, 18 Sep 2007 16:33:14 +0000, Daniel Santos wrote:
Hi,

I've found the problem. I was doing in a script

CMD="cmd ... 2>&1"
CMD_OUTPUT=$($CMD)

CMD_OUTPUT came out empty

Then I did

CMD="cmd ..."
CMD_OUTPUT=$($CMD 2>&1)

et voilá. CMD_OUTPUT has the output.

Can you explain me where is the difference ?
Daniel Santos


> Hi,
>
> I want to run a command and assign any output to a variable. If the
> command is successful it outputs to stdout. If not, it outputs to stderr.
>
> Since I want to parse any of its output I need to redirect stderr to
> stdout because from what I've read command substitution only outputs
> to stdout.
>
> redirecting the commands stderr to stdout with 2>&1 didn't work for me (I'm
> using bash 3.1). I guess its because the command runs in a subshell.
>
> Any ideas ?
> Daniel Santos

Re: stderr and command substitution

am 19.09.2007 21:04:56 von Bill Marcum

Daniel Santos wrote:
>
> On Tue, 18 Sep 2007 16:33:14 +0000, Daniel Santos wrote:
> Hi,
>
> I've found the problem. I was doing in a script
>
> CMD="cmd ... 2>&1"
> CMD_OUTPUT=$($CMD)
>
> CMD_OUTPUT came out empty
>
> Then I did
>
> CMD="cmd ..."
> CMD_OUTPUT=$($CMD 2>&1)
>
> et voilá. CMD_OUTPUT has the output.
>
> Can you explain me where is the difference ?
> Daniel Santos
>
The shell parses redirection characters before variable substitution, so
if 2>&1 is part of a variable, the redirection is not
performed.