LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-11-2004, 01:06 AM   #1
Br. Nicholas
Member
 
Registered: Nov 2000
Location: Buena Vista, Colorado
Posts: 34

Rep: Reputation: 16
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
 
Old 10-11-2004, 01:19 AM   #2
mirradric
Member
 
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188

Rep: Reputation: 31
Code:
man tee
 
Old 10-11-2004, 08:50 AM   #3
Br. Nicholas
Member
 
Registered: Nov 2000
Location: Buena Vista, Colorado
Posts: 34

Original Poster
Rep: Reputation: 16
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
 
Old 10-11-2004, 08:56 AM   #4
mirradric
Member
 
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188

Rep: Reputation: 31
hmm... It works for me...
 
Old 10-11-2004, 09:08 AM   #5
mirradric
Member
 
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188

Rep: Reputation: 31
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.
 
Old 10-11-2004, 09:13 AM   #6
mirradric
Member
 
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188

Rep: Reputation: 31
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.
 
Old 10-11-2004, 10:24 AM   #7
mirradric
Member
 
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188

Rep: Reputation: 31
Maybe you can test using
Code:
echo something | wc
if your output is
Code:
something
0 0 0
then your echo is print to stderr instead of stdout
 
Old 10-12-2004, 12:45 AM   #8
Br. Nicholas
Member
 
Registered: Nov 2000
Location: Buena Vista, Colorado
Posts: 34

Original Poster
Rep: Reputation: 16
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.
 
Old 10-12-2004, 12:57 AM   #9
Br. Nicholas
Member
 
Registered: Nov 2000
Location: Buena Vista, Colorado
Posts: 34

Original Poster
Rep: Reputation: 16
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.
 
Old 10-12-2004, 01:05 AM   #10
mirradric
Member
 
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188

Rep: Reputation: 31
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...
 
Old 10-12-2004, 01:42 AM   #11
mirradric
Member
 
Registered: May 2004
Location: Singapore
Distribution: Debian woody and debian sarge
Posts: 188

Rep: Reputation: 31
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
Code:
alias echo
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.
 
Old 10-12-2004, 03:09 PM   #12
Br. Nicholas
Member
 
Registered: Nov 2000
Location: Buena Vista, Colorado
Posts: 34

Original Poster
Rep: Reputation: 16
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
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can't create file using echo: /proc/sys/net/ipv4/ip_local_port_change rsumbeling Linux - General 4 07-17-2013 04:20 AM
error in gcc / g++ standard file maheshbmane Programming 6 09-02-2005 08:30 PM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 02:59 PM
echo data in to a file xlord Linux - Newbie 3 05-29-2004 12:19 PM
Using echo to output a string to a file with forward slashs chrisk5527 Linux - General 15 06-21-2003 02:04 PM

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

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