我试图回复错误消息并同时将其写入日志文件中,但我不知道该怎么做。我已经使用了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
答案 0 :(得分: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.
答案 1 :(得分:2)
您想要使用' tee'命令:
NAME
tee - read from standard input and write to standard output
and files
SYNOPSIS
tee [OPTION]... [FILE]...
e.g。
$echo "Hello world!" | tee test.txt
Hello world!
$cat test.txt
Hello world!