2016-09-27 54 views
1

我試圖迴應錯誤消息以及在同一時間寫入日誌文件,但我不知道如何去做。我使用了1> & 2,但它只是將它發送到日誌文件並且不回顯消息。這裏是我的代碼:我可以回顯錯誤消息並將其發送到日誌文件中?

while read -r username password; do 
    egrep "^$username" /etc/passwd >/dev/null 
    if [ $? -eq 0 ]; then 
     echo "ERROR BLABLAH $DATE" 1>&2 >> /var/log/error.log 

回答

3

嘗試

echo "ERROR BLABLAH $DATE" | tee -a /var/log/error.log 1>&2 

說明:

tee         # will repeat the std input. 
    -a /var/log/error.log    # will append to the error.log file 
          1>&2  # will send the stdin to stderr. 
+0

謝謝!這就是訣竅。 – Max

2

你想使用 '三通' 命令:

NAME 
tee - read from standard input and write to standard output 
and files 
SYNOPSIS 
tee [OPTION]... [FILE]... 

例如

$echo "Hello world!" | tee test.txt 
Hello world! 
$cat test.txt 
Hello world! 
相關問題