LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-19-2010, 09:38 AM   #1
10110111
Member
 
Registered: Jun 2008
Location: St.-Petersburg, Russia
Distribution: (B)LFS, Ubuntu, SliTaz
Posts: 403

Rep: Reputation: 51
How to get stderr and stdout of a program into separate env variables?


I'm looking for a more efficient way of doing something like this:
Code:
MY_STDOUT=`my-command`
MY_STDERR=`my-command >&2`
That is, i want to have to run my-command only once and get the same result.
I've tried this:
Code:
YYY=$(XXX=`{ echo -n 111; echo 222 >&2; }` 2>&1); echo $XXX $YYY
where "{ echo -n 111; echo 222 >&2; }" is my-command. But it gives this output:
Code:
222
111
instead of "111 222".
What's wrong in my script? Maybe there exists a nicer way of doing this?
 
Old 05-19-2010, 03:35 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 167Reputation: 167
First lets establish some basic behavior...

test.sh
Code:
#!/bin/bash
echo "STDOUT"
echo "STDERR" 1>&2
Code:
core$ ./test.sh
STDOUT
STDERR
core$ ./test.sh 2> STDERR.txt
STDOUT
core$ ./test.sh 1> STDOUT.txt
STDERR
core$ cat STDERR.txt
STDERR
core$ cat STDOUT.txt
STDOUT
So at this point we know and can prove our basic script outputs one line to stderr and one line to stdout. You might want to use something like this for testing because sometimes behavior does change from executing several commands to calling another program due to redirection and piping.

This--

Code:
YYY=$(XXX=`{ echo -n 111; echo 222 >&2; }` 2>&1); echo $XXX $YYY
only gives me 222, which is not being echo'd at all but is being displayed on stderr... you get same result removing the echo as having it. You can tell where your stuff is coming from by redirecting the stream to a file 1>file or 2>file.

The real problem you're having here is scoping. the XXX is being lost when it returns from calling out no matter what else you do.
 
Old 05-20-2010, 06:17 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Backing up what rweaver wrote, as it says in the Bash FAQ: "What you cannot do is capture stdout in one variable, and stderr in another, using only FD redirections. You must use a temporary file to achieve that one".
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash Script: parse active process stderr, strip, dump into variables, use variables TimeFade Programming 1 02-13-2010 06:09 AM
Can't seem to catch some output from a program - not stdout or stderr. uhh.. help? BrianK Programming 2 01-30-2010 11:53 AM
stderr vs stdout sniffer_raghav Programming 2 09-05-2008 10:00 AM
stdout,stderr makes program crash surfchen Linux - Newbie 3 03-11-2008 05:27 AM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 02:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration