LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-08-2015, 05:51 PM   #1
usao
Member
 
Registered: Dec 2011
Location: Chandler, AZ
Posts: 286

Rep: Reputation: Disabled
pipe on other than stdout


I have a program which outputs using multiple streams.
I specifically need to get a count of the number of lines going to stderr.
Im looking for a way to pipe the stderr output to a wc command, while still displaying stdout normally.
The only way I know how to do this now is to redirect stderr to a file, then run a wc on the file as a second step, but I don't see any way to do this without using a file.

Is there a way to get a pipe to work with other streams than stdin/stdout?
 
Old 10-08-2015, 06:22 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Code:
exec 3>&1
ls foo . 2>&1 >&3 | wc
Substitute your own command for "ls foo .", which is just an example of something that sends something to both stdout and stderr (assuming that there are some ordinary names in the current directory and that "foo" is not one of them).

The "exec 3>&1" creates file descriptor 3 as a copy of the current fd 1. It's a temporary variable to save fd 1.

The ordering of the redirections on the next line is critical. The first thing processed is the pipe symbol separating the two commands, redirecting the first command's fd 1 to the pipe. Now, the redirections for the first command are processed from left to right. First, fd 2 is made a copy of the command's current fd 1, i.e., the pipe. Finally, fd 1 is made a copy of fd 3, which is the original stdout. The net result is that the command's stderr is sent to the pipe and stdout is sent to the same place it was going before all this redirection.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
tar - write to stdout & create log files ( from stdout and stderr ) [solved] paziulek Linux - General 2 02-23-2014 12:26 PM
How to redirect standard stdout to multi stdout ( Bash )? john.daker Programming 4 11-03-2008 11:20 PM
redirecting stdout to /dev/null and stderr to stdout? Thinking Programming 1 05-18-2006 02:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 04:43 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