问题描述
我在'reto'成员的注释中遇到了以下命令,该命令将完成我想做的大部分事情。不幸的是,我刚加入就无法发表评论。
他们做了我想做的大部分事情,但我也想在某些时候在脚本中将标准输出重定向到屏幕上。如果if语句失败,则要求用户提供用户名和密码。然后,一旦用户交互完成,即可将标准输出恢复为日志记录。
任何帮助都会很棒。 更新
#this works for me
LOGFILE=./ClamAV_install_script.log
exec 3>&1 >$LOGFILE 2> >(tee -a $LOGFILE >&2)
# Everything below will go to the file 'ClamAV_install.log':
date
#all these go to screen
{
tail -40 ./ClamAV_install_script.log
#To give option to scan full system
printf "\n\n\nDo you wise to scan the full file system / ? \n"
read -p 'Type Y to scan: ' Conformation
printf "\nYou have entered: $Conformation\n"
} >&3
#!/bin/bash
set -e
outfile=logfile
exec > >(cat >> $outfile)
exec 2> >(tee -a $outfile >&2)
#STDOUT和STDERR将被写入$ outfile,在控制台上只能看到STDERR
解决方法
您可以通过在重定向之前将流复制到另一个文件描述符中来保存流。
$: exec 3>&1 >>$outfile 2> >(tee -a $outfile >&2)
$: date # goes to log
$: date >&3 # goes to console
Tue,Sep 15,2020 8:24:19 AM
c.f。 https://www.gnu.org/software/bash/manual/html_node/Redirections.html