Say, this is my shell script
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"
So, I want to store the whole script output in a log file (say "complete-output.log")
And on my stdout --- I want to show only some cherry-picked items (Ex. some echo messages)
I used named pipes,
# 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
So, I am getting complete output on both (In file, as well as stdout)
But, I do not want stdout to be so verbose.. only want to show a few things there !
What is the correct way to do so ? Thanks in advance !
CodePudding user response:
Can this achieve what you wanted ?
#!/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"
Run it with
$ 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