Shell 脚本:想要在 stdout 中显示一些详细信息 &日志文件中的所有详细信息



说,这是我的shell脚本

echo "Show this on stdout and logfile"
wget -O .......  # "Only in logfile"
echo "Show this on stdout and logfile"
cp file1.txt     # "Only in logfile"

因此,我想将整个脚本输出存储在日志文件中(例如"complete-output.log")

在stdout上——我只想显示一些精心挑选的项目(例如一些echo消息)


我使用了命名管道,

# Set up a named pipe for logging
npipe=logpipe
mknod $npipe p
# Log all output to a log for error checking
sudo tee <$npipe /var/log/complete-output.log &
exec 1>$npipe 2>&1
# Deleting named pipe on script EXIT
trap 'rm -f $npipe' EXIT

所以,我得到了完整的输出(In文件,以及stdout)


但是,我不希望stdout如此冗长…我只想在那里展示一些东西!

正确的做法是什么?提前感谢!

这能达到你想要的效果吗?

#!/usr/bin/env bash
echo2(){
echo "$@"
echo "$@" > /dev/tty
}
exec > /var/log/complete-output.log 2>&1
echo2 "Show this on stdout and logfile"
echo wget -O .......  # "Only in logfile"
echo2 "Show this on stdout and logfile"
echo cp file1.txt     # "Only in logfile"

运行
$ bash test.sh
Show this on stdout and logfile
Show this on stdout and logfile
$ cat /var/log/complete-output.log
Show this on stdout and logfile
wget -O .......
Show this on stdout and logfile
cp file1.txt

相关内容

最新更新