echo to stderr and tee to log file?

前端 未结 4 1603
青春惊慌失措
青春惊慌失措 2020-12-16 14:52

In bash script,

echo \"error\" 1>&2 | tee -a log

will print stderr in screen but no log to file, how to do these at same tim

相关标签:
4条回答
  • 2020-12-16 15:19

    To echo the text to both the log file and stderr, but not stdout, try this:

    echo "error" | tee -a log 1>&2
    
    0 讨论(0)
  • 2020-12-16 15:25

    To view both stdout and stderr on the console and send both streams to a log, redirect stderr to stdout as shown below:

    progam.sh 2>&1 | tee -a log
    
    0 讨论(0)
  • 2020-12-16 15:33

    But default only stdout is passed along in pipes, so that in

    $ echo "error" | tee 
    

    tee only sees the stout from the echo, not the stderr. stderr will still be displayed in the terminal.

    0 讨论(0)
  • 2020-12-16 15:35
    echo "error" 1>&2 | tee -a log
    

    With the first part 1>&2, what you are saying is: "Redirect stdout to stderr". So the echoed output "error" goes to stderr.

    Pipe (|) only reads from stdout, not stderr. So tee doesn't get any stdin at all from the pipe. Hence, it appends nothing to the log file.

    0 讨论(0)
提交回复
热议问题