Max Max - 1 year ago 97
Linux Question

I can I echo error message and send it into a log file?

I'm trying to echo the error message as well as writing it in a log file at the same time but I'm not sure how to do it. I've used 1>&2, but it just sends it to the log file and doesn't echo the message. Here's my code:

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

Answer Source


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.  
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download