openSUSE Forums > Programming/Scripting » Bash: Can stdout go to terminal and pipe?

Go Back   openSUSE Forums > Programming/Scripting
Forums FAQ Members List Search Today's Posts Mark Forums Read

Programming/Scripting Questions about programming, bash scripts, perl, php, cron jobs, ruby, python, etc.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 01-Jul-2009, 17:12
Student Penguin
 
Join Date: Nov 2008
Posts: 50
opensuseforumorg42 hasn't been rated much yet
Question Bash: Can stdout go to terminal and pipe?

Code:
cat MailingList | cut -d, -f1 | sort -u | wc -l
An example command is shown above. The output would be something like:

name1
name2
2

I'd like the output from the sort to go to the terminal as well as to the pipe.

I've used tee before, but I want all output to go to the terminal. I did some googling and scanned the bash doc at tldp which lend me down the path of trying a combination of parenthesis around the commands (subshell) and file descriptor redirecting. I haven't been successful. Any thoughts?

Thank you.
Reply With Quote
  #2 (permalink)  
Old 01-Jul-2009, 18:42
Flux Capacitor Penguin
 
Join Date: Jun 2008
Location: GMT+10
Posts: 6,088
ken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputationken_yap has a brilliant future with this reputation
Default Re: Bash: Can stdout go to terminal and pipe?

Can't be done using bash redirections alone, you need a process to do the splitting.

Try:

Code:
cut -d, -f1 < MailingList | sort -u | tee /dev/stdout | wc -l
I corrected that common tendency which you exemplified, to start a pipe with cat from a file.
Reply With Quote
  #3 (permalink)  
Old 01-Jul-2009, 18:51
Busy Penguin
 
Join Date: Jun 2008
Posts: 410
RedDwarf is a reputation jewel in the roughRedDwarf is a reputation jewel in the roughRedDwarf is a reputation jewel in the roughRedDwarf is a reputation jewel in the roughRedDwarf is a reputation jewel in the rough
Default Re: Bash: Can stdout go to terminal and pipe?

Code:
cut -d, -f1 < MailingList | sort -u | tee >(wc -l)
Reply With Quote
  #4 (permalink)  
Old 02-Jul-2009, 11:25
Student Penguin
 
Join Date: Nov 2008
Posts: 50
opensuseforumorg42 hasn't been rated much yet
Default Re: Bash: Can stdout go to terminal and pipe?

So redirect the output of the tee to a subshell, that's one I totally missed. Argh. A subshell is definitely needed?

I was reading things that were swapping, or is that duplicating, file descriptors like: 3>&2 2>&1 1>&3. If tee didn't exist, is it possible?

Using cat is a bad habit that keeps creeping. Redirection, pipes, file descriptors, subshells and their environment, is something I'm working on getting better at.

Thanks all.
Reply With Quote
  #5 (permalink)  
Old 02-Jul-2009, 12:44
Busy Penguin
 
Join Date: Jun 2008
Posts: 410
RedDwarf is a reputation jewel in the roughRedDwarf is a reputation jewel in the roughRedDwarf is a reputation jewel in the roughRedDwarf is a reputation jewel in the roughRedDwarf is a reputation jewel in the rough
Default Re: Bash: Can stdout go to terminal and pipe?

You could
Code:
cut -d, -f1 < MailingList | sort -u | tee /dev/stderr | wc -l
Isn't exactly the same, since stdout isn't going to the terminal. But if you are only worried about it arriving to the terminal, no matter if from stdout or stderr, then it works.

And you can also
Code:
exec 3>&1; cut -d, -f1 < pendientes | sort -u | tee /dev/fd/3 | wc -l; exec 3>&-
Once again what goes to the terminal isn't stdout but the file descriptor #3. But in this case the output will not be mixed with stderr.


Tee must be used yes or yes. The '|' redirects the stdout to the pipe, so stdout doesn't goes to the terminal anymore. If you want it to go to two different places you need two copies, that's what tee does.
Your "3>&2 2>&1 1>&3" puts stdout in stderr and stderr in stdout. But since stderr is empty...
Reply With Quote
Reply

Bookmarks


Thread Tools
Display Modes

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




 

Search Engine Friendly URLs by vBSEO 3.3.2