LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Output Redirection - Trying to output to screen and file? (http://www.linuxquestions.org/questions/linux-newbie-8/output-redirection-trying-to-output-to-screen-and-file-712294/)

helptonewbie 03-17-2009 12:16 PM

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

openSauce 03-17-2009 12:24 PM

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
~$


helptonewbie 03-17-2009 01:41 PM

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

openSauce 03-17-2009 02:06 PM

/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.

x_terminat_or_3 03-17-2009 06:03 PM

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.

openSauce 03-17-2009 06:08 PM

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.

helptonewbie 03-18-2009 07:59 AM

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

helptonewbie 03-19-2009 07:05 AM

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


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