LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 01-23-2012, 11:13 AM   #1
nenpa8lo
Member
 
Registered: Oct 2008
Location: Ireland
Distribution: Slackware 13.0, Slackware 13.37
Posts: 68
Blog Entries: 2

Rep: Reputation: 18
tee alternative


I am working with very limited shell (busybox) which has no 'tee' command. Therefore I need an alternative. Task is to print stdout to a file, and stderr to console and the file.
Any ideas?
 
Old 01-23-2012, 11:23 AM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
How about a script like this? That's about the simplest thing I can think of right now

Code:
FILENAME='./log'

while read TEMP_LINE; do
	echo "$TEMP_LINE"
	echo "$TEMP_LINE" >> $FILENAME
done

Last edited by millgates; 01-25-2012 at 05:06 AM. Reason: It's supposed to be >>, not >
 
Old 01-23-2012, 01:08 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
The GNU awk manual has an example of tee in awk:

13.2.5 Duplicating Output into Multiple Files
 
Old 01-24-2012, 09:56 AM   #4
nenpa8lo
Member
 
Registered: Oct 2008
Location: Ireland
Distribution: Slackware 13.0, Slackware 13.37
Posts: 68

Original Poster
Blog Entries: 2

Rep: Reputation: 18
I think I need some help here...

stdout&stderr ===> debugfile
stderr ===> console

Code:
cmd | tee file.txt
Prints stderr/stdout to the console and stdout to the file. I need to change it so it works like on the image above.
 
Old 01-24-2012, 11:04 AM   #5
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
How about simply rebuilding busybox with tee enabled? Shouldn't add but a few bytes.
--- rod.
 
Old 01-24-2012, 12:38 PM   #6
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
I was hoping to see some fancy shell fd re-direction tick here... still hoping, infact.
 
Old 01-24-2012, 12:43 PM   #7
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by nenpa8lo View Post
I think I need some help here...

stdout&stderr ===> debugfile
stderr ===> console

Code:
cmd | tee file.txt
Prints stderr/stdout to the console and stdout to the file. I need to change it so it works like on the image above.
How about something like this?

Code:
command  3>&1 1>&2 2>&3 | tee file.txt
 
Old 01-25-2012, 04:05 AM   #8
nenpa8lo
Member
 
Registered: Oct 2008
Location: Ireland
Distribution: Slackware 13.0, Slackware 13.37
Posts: 68

Original Poster
Blog Entries: 2

Rep: Reputation: 18
@millgates It prints stdout to the console and stderr to the console and the logfile. I need it to work the other way round:

stderr+stdout => file
stderr => console
 
Old 01-25-2012, 05:04 AM   #9
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Oh, sorry... I seem to have misread your question. How about this then?

Code:
cmd 3>&1 1>>logfile.txt 2>&3 | tee -a logfile.txt
 
1 members found this post helpful.
Old 01-25-2012, 05:19 AM   #10
nenpa8lo
Member
 
Registered: Oct 2008
Location: Ireland
Distribution: Slackware 13.0, Slackware 13.37
Posts: 68

Original Poster
Blog Entries: 2

Rep: Reputation: 18
SOLVED! You're the man millgates.
 
Old 01-26-2012, 03:39 AM   #11
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Quote:
Originally Posted by millgates View Post
Code:
cmd 3>&1 1>>logfile.txt 2>&3 | tee -a logfile.txt
Couldn’t it be shortend to:
Code:
cmd 2>&1 >>logfile.txt | tee -a logfile.txt
 
1 members found this post helpful.
Old 01-26-2012, 10:51 AM   #12
nenpa8lo
Member
 
Registered: Oct 2008
Location: Ireland
Distribution: Slackware 13.0, Slackware 13.37
Posts: 68

Original Poster
Blog Entries: 2

Rep: Reputation: 18
Both versions work, thanks!
But can you explain me why? How is stderr getting to the console and the file, and normal output to the file?
Thanks
 
Old 01-26-2012, 11:01 AM   #13
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
The entries must be read from right to left:
Code:
2>&1
means: copy the file descriptor to which 1 is pointing to 2, i.e. the stderr will go to the stdout. Then the:
Code:
>>logfile.txt
will copy the file descriptor from logfile.txt to 1, i.e. stdout is written to the file. Often it’s used in the reverse order to combine stdout and stderr.

The tee command will then append the original stderr (which is now going via the stdout descriptor) to the file logfile.txt.

Unless there would be a huge delay because of the tee command, there should be no race condition where both redirections will write at the same time to the file.
 
  


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
tee command implementation hellorajni Linux - Software 2 11-10-2010 12:54 PM
how does tee command implemented? hellorajni Linux - Newbie 1 11-04-2010 02:51 PM
(e)grep and tee question Vhal Linux - Newbie 7 05-27-2010 06:46 AM
tee for two JohnnyBoy123 Linux - Newbie 3 04-22-2009 07:48 PM
tee vs > or >> DotHQ Linux - General 3 08-23-2006 01:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:06 PM.

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