|
Home > Archive > Unix Shell > September 2007 > stderr and command substitution
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
stderr and command substitution
|
|
| Daniel Santos 2007-09-18, 1:27 pm |
| 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
| |
| Alan Curry 2007-09-18, 7:19 pm |
| In article <46effdca$0$26760$a729d347@news.telepac.pt>,
Daniel Santos <dlds@sapo.pt> 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
| |
| Ed Morton 2007-09-18, 7:19 pm |
| 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.
| |
| Daniel Santos 2007-09-19, 7:30 am |
| 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
| |
| Bill Marcum 2007-09-19, 7:19 pm |
| 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.
|
|
|
|
|