Home Write to terminal after redirecting stdout to a file without using stderr?
Reply: 2

Write to terminal after redirecting stdout to a file without using stderr?

Dagg Nabbit
1#
Dagg Nabbit Published in 2012-02-23 00:00:27Z
 I have two shell scripts, one that serves as the main "program" and another that serves as a "library." In several places in the "program," I'll do something like: log "$thing" >> "$logfile", where log is a function defined in the "library." # program.sh logfile="log.txt" stuff="hahah heheh hoho" . library.sh for thing in $stuff; do log "$thing" >> "$logfile" done  My question: Is there a way to redirect some of the output from the function back to the terminal without using stderr? # library.sh log () { # This gets written to the log echo "date --rfc-3339=seconds:$1" # How to write this to the terminal *without* using stderr? echo "Info: Message written to log." >&2 }  I want to avoid the use of stderr because in my actual program, there's an option to redirect errors to a file, but the messages I want to send to the terminal are informational, not errors, and should always show up on the terminal.
Ignacio Vazquez-Abrams
2#
Ignacio Vazquez-Abrams Reply to 2012-02-23 00:05:42Z
 Open /dev/tty on another FD. exec 0< /dev/null exec 1> /dev/null exec 2> /dev/null exec 3> /dev/tty echo 'Hello, World!' >&3 
sarnold
3#
sarnold Reply to 2012-02-23 00:19:52Z
 You can write directly to /dev/tty each time you want to write to the terminal: echo "hello world" > /dev/tty  For a small example: $cat writer.sh #!/bin/sh echo "standard output" echo "standard error" >&2 echo "direct to terminal" > /dev/tty$ ./writer.sh > /tmp/out 2> /tmp/err direct to terminal $cat /tmp/out standard output$ cat /tmp/err standard error \$ 
 You need to login account before you can post.
Processed in 0.306408 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO