LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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
 
LinkBack Search this Thread
Old 03-19-2007, 03:58 PM   #1
ask4info
LQ Newbie
 
Registered: Mar 2007
Location: switzerland
Distribution: redhat-fedora,suse,various
Posts: 3

Rep: Reputation: 0
separte pipe for stderr and stout in a bash script


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
 
Old 03-19-2007, 04:09 PM   #2
cfaj
Member
 
Registered: Dec 2003
Location: Toronto, Canada
Distribution: Mandriva, Ubuntu, LFS, gNewSense
Posts: 221

Rep: Reputation: 31
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 04:11 PM.
 
Old 03-20-2007, 01:07 AM   #3
ask4info
LQ Newbie
 
Registered: Mar 2007
Location: switzerland
Distribution: redhat-fedora,suse,various
Posts: 3

Original Poster
Rep: Reputation: 0
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
 
Old 03-20-2007, 03:58 AM   #4
ask4info
LQ Newbie
 
Registered: Mar 2007
Location: switzerland
Distribution: redhat-fedora,suse,various
Posts: 3

Original Poster
Rep: Reputation: 0
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...!
 
Old 03-24-2007, 08:04 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: Slackware64 13.37, Kubuntu 10.04
Posts: 2,941

Rep: Reputation: Disabled
Code:
#!/bin/bash
echo "NORMAL!" > /proc/self/fd/1
echo "ERROR!" > /proc/self/fd/2
ta0kira
 
Old 03-24-2007, 08:09 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: Slackware64 13.37, Kubuntu 10.04
Posts: 2,941

Rep: Reputation: Disabled
Wait, maybe I'm misunderstanding. Will this work?
Code:
( ./your-script > ./stdout_pipe ) 2> ./stderr_pipe
ta0kira
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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


All times are GMT -5. The time now is 10:54 AM.

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
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration