linux - 我可以回显错误消息并将其发送到日志文件中吗?

标签 linux bash error-handling echo

我正在尝试回显错误消息并同时将其写入日志文件,但我不确定该怎么做。我已经使用了 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

最佳答案

尝试

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.  

关于linux - 我可以回显错误消息并将其发送到日志文件中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39731908/

相关文章:

linux - 将非目录复制到目录和 vv 上

linux - Fortran gfortran linux 中的 "Segmentation Fault (core dumped)"错误

java - 我们是否应该记录 HTTP 400 的堆栈跟踪

linux - 在 perl 中运行 linux sftp 命令不起作用

linux - 删除特定的 iptables 规则

bash - 在 sed 命令中使用命令行参数

linux - Bash 输出发生在提示之后,而不是之前,这意味着我必须手动按 enter

mysql - 在无人参与的 bash 脚本中使用 Mysql 时保护密码

bash - Bash脚本错误检查

PHP/MySQL : What should I use to manage errors?