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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-11-2004, 01:06 AM
|
#1
|
|
Member
Registered: Nov 2000
Location: Buena Vista, Colorado
Posts: 31
Rep:
|
How do I echo to both standard out and to a file?
Does anyone know of a way to echo to both standard out and to a file at the same time?
I'm writing a script, and I'd like to echo text to both standard out and append it to a log file, preferably at the same time. Is there any way I can do that in one command?
I've tried things like
echo "test echo" 2>> logfile
and a few things with piping |
But nothing seems to work.
I'd greatly appreciate any help anyone can give me.
With kind regards,
Peter, hieromonk
|
|
|
|
10-11-2004, 01:19 AM
|
#2
|
|
Member
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188
Rep:
|
|
|
|
|
10-11-2004, 08:50 AM
|
#3
|
|
Member
Registered: Nov 2000
Location: Buena Vista, Colorado
Posts: 31
Original Poster
Rep:
|
Thank you, but unfortunately, this doesn't seem to work with the echo command. "tee" works fine with things like the "date" command, but if you try to use it with the "echo" command, it just gets echoed along with everything else.
eg.
echo 'test echo' tee -a log.file
echoes "test echo tee -a log.file" to stdout, and writes nothing in the log file.
echo 'test echo' | tee -a log.file
echoes "test echo" to stdout, and writes nothing in the log file.
So how do I get this to work with echo? And yes, I've already looked at "man echo", and it didn't help. I *always* search before I ask...
Again, I hope someone can help me.
fp
|
|
|
|
10-11-2004, 08:56 AM
|
#4
|
|
Member
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188
Rep:
|
hmm... It works for me...
|
|
|
|
10-11-2004, 09:08 AM
|
#5
|
|
Member
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188
Rep:
|
Re: How do I echo to both standard out and to a file?
If it's printing to stdout then it is piping and tee is getting the pipe*. Somehow it's not writing it to the file. Do you have write permission to the file? But it works for other commands.... I have no idea.
edit: *The other posibility is that the pipe is entirely broken.
Last edited by mirradric; 10-11-2004 at 09:09 AM.
|
|
|
|
10-11-2004, 09:13 AM
|
#6
|
|
Member
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188
Rep:
|
could you have by some chance permanently redirected echo's stdout to stderr.
say do you have
alias echo="echo >&2"
somewhere? I tried this and I got your result.
|
|
|
|
10-11-2004, 10:24 AM
|
#7
|
|
Member
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188
Rep:
|
Maybe you can test using
Code:
echo something | wc
if your output is then your echo is print to stderr instead of stdout
|
|
|
|
10-12-2004, 12:45 AM
|
#8
|
|
Member
Registered: Nov 2000
Location: Buena Vista, Colorado
Posts: 31
Original Poster
Rep:
|
Please forgive me, but I got called away... unfortunately for the day. But with both bosses away now, that shouldn't happen again for a while.
mirradric -- exactly what worked for you?
Yes, I have permission for the file. I'm running as root.
How would I know if the pipe is broken? And how would I fix it?
How would I know if I have alias echo="echo >&2" somewhere? Where would I need to look?
echo something | wc gives me
1 1 10
What does that mean?
I really appreciate your help, by the way. 
|
|
|
|
10-12-2004, 12:57 AM
|
#9
|
|
Member
Registered: Nov 2000
Location: Buena Vista, Colorado
Posts: 31
Original Poster
Rep:
|
BTW, I don't know if it is related, but I have another script that I run with a cron job which always gives me this error at the end:
stderr is not a tty - where are you?
Any ideas?
I'm running slack 9.1, if it matters.
|
|
|
|
10-12-2004, 01:05 AM
|
#10
|
|
Member
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188
Rep:
|
Quote:
Originally posted by Br. Nicholas
echo something | wc gives me
1 1 10
What does that mean?
|
It means that the output from echo is indeed piping to wc and the pipe works fine.
Quote:
|
mirradric -- exactly what worked for you?
|
I'm saying that
Code:
echo something | tee -a logfile
works for me.
Let me try to think of something...
|
|
|
|
10-12-2004, 01:42 AM
|
#11
|
|
Member
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188
Rep:
|
I'm at a lost, but let's try to diagnose and narrow down the problem. Something might pop up along the way.
First, you mentioned that
Code:
date | tee -a logfile
works, so the problem is not likely to be either tee or bash's pipes. Maybe you can try other programs and see if they work as well. Anyway so far the problem seems to be with echo.
Second,
Code:
#echo something | wc
1 1 10
seems to imply that echo is indeed printing to stdout.
Maybe you can try to see if any alias for echo have been defined.
Also check that you get the following results (which I think you'll get based on the wc test)
Code:
#echo something > filea
#echo something 2> fileb
#cat filea
something
#cat fileb
#
Does echo something | tee -a logfile work as a normal user but not as root?
BTW, I'm not implying that you run the commands as root by using # as the shell prompt. Run them as whichever acct that gives you the problem.
got to go for tutorial. will check back with you if I think of anything else.
Last edited by mirradric; 10-12-2004 at 01:51 AM.
|
|
|
|
10-12-2004, 03:09 PM
|
#12
|
|
Member
Registered: Nov 2000
Location: Buena Vista, Colorado
Posts: 31
Original Poster
Rep:
|
I thought I tried this (echo something | tee -a logfile
), but apparently I must not have. I was very tired last night when I finally was able to look at this problem again, so maybe I didn't. But this seems to work fine.
Thank you very, very much. I was able to cut about 50 lines of code out of this script, and it makes it a lot easier to understand.
I appreciate all of your help very, very much.
With very kind regards,
Peter, hieromonk
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 10:26 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|