LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Thread Tools
Old 03-19-2007, 04:58 PM   #1
ask4info
LQ Newbie
 
Registered: Mar 2007
Location: switzerland
Distribution: redhat-fedora,suse,various
Posts: 3
Thanked: 0
separte pipe for stderr and stout in a bash script


[Log in to get rid of this advertisement]
hi all

what's the best way to output the stderr and stdout of a shell command thru different pipes?
I want to process the stderr different from stout, generated by a command. How to combine both and still have the same output descriptors? In other words, how
to combine ("a one liner"!) the two commands:

command 2>&1 | .. pipe 1 .. -> sterr (&2)
command | .. pipe 2 ... -> stdout (&1)

any ideas?

thanks
ask4info is offline     Reply With Quote
Old 03-19-2007, 05:09 PM   #2
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mandriva, Ubuntu, LFS, gNewSense
Posts: 221
Thanked: 0
Quote:
Originally Posted by ask4info
hi all

what's the best way to output the stderr and stdout of a shell command thru different pipes?
I want to process the stderr different from stout, generated by a command. How to combine both and still have the same output descriptors? In other words, how
to combine ("a one liner"!) the two commands:

command 2>&1 | .. pipe 1 .. -> sterr (&2)
command | .. pipe 2 ... -> stdout (&1)

any ideas?
Use a named pipe. This is a very basic example:
Code:
q()
{
  echo 1_$RANDOM
  echo 2_$RANDOM >&2
}

mkfifo stdo
cat stdo &
q 2>stdo | cat
rm stdo
Or save stderr to a regular file and have your command read from that.


Last edited by cfaj; 03-19-2007 at 05:11 PM..
cfaj is offline     Reply With Quote


Old 03-20-2007, 02:07 AM   #3
ask4info
LQ Newbie
 
Registered: Mar 2007
Location: switzerland
Distribution: redhat-fedora,suse,various
Posts: 3
Thanked: 0

Original Poster
hi cfai

thanks for your sample.

thats what i did too however i was wondering wether there is a "smart" way to do it with e.g. the 'exec' command somehow. The sample below is now my final:

#!/bin/bash

q()
{
echo 1_stdout
echo 2_stderr >&2
}

p()
{
rm -fr /tmp/stdo
mkfifo /tmp/stdo
cat /tmp/stdo | sed 's|err|ERR|g' >&2 &
q 2>/tmp/stdo | sed 's|out|OUT|g'
rm -fr /tmp/stdo
}

echo -n "sterr: "
p 1>/dev/null
echo -n "stout: "
p 2>/dev/null

Note: what was missing was the redirection of the named pipe to &2

have a nice day
ask4info is offline     Reply With Quote


Old 03-20-2007, 04:58 AM   #4
ask4info
LQ Newbie
 
Registered: Mar 2007
Location: switzerland
Distribution: redhat-fedora,suse,various
Posts: 3
Thanked: 0

Original Poster
hi cfai

i just found out a better way using the designator swapping technic:

#!/bin/bash

cmd()
{
echo 1_stdout
echo 2_stderr 1>&2
}

out1() # different sub shell
{
( ( cmd | sed 's|out|OUT|g' ) 3>&1 1>&2 2>&3 | sed 's|err|ERR|g' ) 3>&1 1>&2 2>&3
# \----swap----/ |-> stout=stderr \----swap----/
}

out2() # current sub shell
{
{ { cmd | sed 's|out|OUT|g'; } 3>&1 1>&2 2>&3 | sed 's|err|ERR|g'; } 3>&1 1>&2 2>&3
# \----swap----/ |-> stout=stderr \----swap----/
}

echo -n "sterr: "
out1 1>/dev/null
echo -n "stout: "
out1 2>/dev/null


until next question...!
ask4info is offline     Reply With Quote


Old 03-24-2007, 09:04 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: Slackware 11.0, Slackware 12.1, slamd64 12.2, Ubuntu Server 8.04, Kubuntu 9.04
Posts: 2,081
Thanked: 39
Code:
#!/bin/bash
echo "NORMAL!" > /proc/self/fd/1
echo "ERROR!" > /proc/self/fd/2
ta0kira
ta0kira is offline     Reply With Quote


Old 03-24-2007, 09:09 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: Slackware 11.0, Slackware 12.1, slamd64 12.2, Ubuntu Server 8.04, Kubuntu 9.04
Posts: 2,081
Thanked: 39
Wait, maybe I'm misunderstanding. Will this work?
Code:
( ./your-script > ./stdout_pipe ) 2> ./stderr_pipe
ta0kira
ta0kira is offline     Reply With Quote



Reply

Bookmarks


Thread Tools

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
Shell script pipe input - bash mostly laikos Programming 4 11-09-2008 06:14 PM
Shell script - stdout & stderr to terminal and file jantman Linux - Software 1 12-07-2006 05:34 PM
bash stderr redirect question code-breaker Linux - Software 1 08-06-2006 05:44 AM
Redirecting error messages to stderr and file in Bash lowpro2k3 Programming 1 04-14-2005 07:47 PM
Read the output from a pipe with bash ? fluppi Linux - Software 3 01-13-2004 01:59 PM


All times are GMT -5. The time now is 08:07 PM.

Main Menu
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
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration