LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-17-2009, 12:16 PM   #1
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Rep: Reputation: 39
Output Redirection - Trying to output to screen and file?


Hi All,

I was hoping to be able to redirect output of a script to the screen and a file as well. I'd like to redirect 2>&1 so stdout and stderr to the screen and also to a file/pipe.
As follows i thought the following would work but it only seems to put stdout to screen and stderr to file/pipe...

Code:
/home/blah/script.sh 3>&1 >&2 2>&3 3>&- | /bin/logger -p daemon.debug -t "script.sh"
Now this is sending data to my central log server that is all working ok, but i'd like both stderr and stdout to be sent to the screen and also to the 'logger' command. I don't think 'tee' is the thing for me (maybe i'm wrong) but its no good because i can't send data to /dev/pts/2 and such because the script isn't going to be running as root either.

Cheers,
M
 
Old 03-17-2009, 12:24 PM   #2
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
Don't think you need to be running as root - this worked for me:

Code:
~$ echo some output | tee /dev/pts/7 | cat > output-file.txt
some output
~$ cat output-file.txt 
some output
~$
 
Old 03-17-2009, 01:41 PM   #3
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Original Poster
Rep: Reputation: 39
Quote:
Don't think you need to be running as root - this worked for me:

Code:

~$ echo some output | tee /dev/pts/7 | cat > output-file.txt
some output
~$ cat output-file.txt
some output
~$
thanks for the reply but that doesn't work.... i think i may have actually meant something like /dev/stderr OR /dev/fd/[0 OR 1 OR 2 OR 3] they are the stdin stdout and stderr files as such(I THINK?!? not that you can call them files as such either)

I have no /dev/pts/7 for one, and that also won't pick up on stderr output.

Regards,
M
 
Old 03-17-2009, 02:06 PM   #4
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
/dev/pts/7 was just the terminal I happened to be using - you can find the file name of the current terminal with tty.

Try
Code:
this_terminal=$(tty)
echo stuff | tee $this_terminal | cat > output-file
and then
Code:
this_terminal=$(tty)
/home/blah/script.sh 2>&1 | tee $this_terminal | /bin/logger -p daemon.debug -t "script.sh"
You should just be able to do tee $(tty) without bothering with the $this_terminal variable, but that's not working for me, no idea why.
 
Old 03-17-2009, 06:03 PM   #5
x_terminat_or_3
Member
 
Registered: Mar 2007
Location: Plymouth, UK
Distribution: Fedora Core, RHEL, Arch
Posts: 342

Rep: Reputation: 38
Quote:
You should just be able to do tee $(tty) without bothering with the $this_terminal variable, but that's not working for me, no idea why.
Because your tee's input stream is attached to the output of the command you are piping, and not to your virtual terminal.
 
Old 03-17-2009, 06:08 PM   #6
openSauce
Member
 
Registered: Oct 2007
Distribution: Fedora, openSUSE
Posts: 252

Rep: Reputation: 39
Just worked that out this minute, but thanks!

Ah actually I just realised my explanation in that thread is wrong. Thanks for your better answer.
 
Old 03-18-2009, 07:59 AM   #7
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Original Poster
Rep: Reputation: 39
Hi There,

Thanks for all help provided from this.. i've looked through the other post as well. Testing of it looks to be working i'll give it a go now and i'll post how i get on.

Code:
command.sh 2>&1 | tee $tty | /bin/logger ....
OK - just run the test
We're almost there... all the data is getting to the mysql database OK which is great. But i'm seeing nothing go to the screen (tty)?

Cheers,
M

Last edited by helptonewbie; 03-18-2009 at 08:13 AM.
 
Old 03-19-2009, 07:05 AM   #8
helptonewbie
Member
 
Registered: Aug 2006
Location: England Somewhere
Distribution: Mandriva, PCLinuxOS, Karoshi, Suse, Redhat, Ubuntu
Posts: 518

Original Poster
Rep: Reputation: 39
Ok to approach this from another angle what could perhaps get this:-
command.sh 2>&1 | tee `/bin/logger ....`

To me the output would be sent to the command 'logger' and also to the shell. But I know 'tee' won't work sending to a command?

The only way i can see that working would be to create a 'fifo' and then get tee to send output to that and have logger attached to that fifo... but this seems stupidly long to do something which is surly simple?

M
 
  


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
redirect screen output to file timbuck Linux - Software 5 12-09-2005 06:57 PM
Output redirection rmartine Linux - Newbie 2 01-25-2005 06:31 PM
the sound gives output when using mic but no output when run a music file medo Debian 0 04-19-2004 07:17 PM
Log Screen Output to a file sdandeker Linux - Newbie 3 09-17-2003 02:57 AM
Output redirection legtester Linux - General 4 07-07-2003 02:36 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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