0

have several commands in a shell file that I would like to log and show on screen. but some commands results I want to NOT show on screen and some I want to - but all of them need to be logged.

I can use tee or > or >> etc..

script.sh | tee -a logfile

but that does not allow me to pick and choose what shows on screen and what goes into logs.

example script - what I have now (each line is different and looks inefficient)

echo "setting date" | tee log.txt #show on screen and log
`date` | tee -a log.txt # screen and log
echo "setting name" | tee -a log.txt #show on screen  
`who am i` >> log.txt | only log

I have a several commands like this - and am wondering if there is a efficient way to append to log AND/OR append to log while showing on screen.

OR do I have to modify and make a call in each line ?

Users will not be able to modify this script.

hypermails
  • 726
  • 1
  • 10
  • 28
  • ps: I understand using exec >> --> but that won't display anything. and i want to be selective in what is displayed and what does not. – hypermails Feb 18 '16 at 17:58
  • So.... on the commands you don't want to log, don't use `tee`, and the ones you do want to log, use `tee`. What more are you looking for? Perhaps I don't understand the question. – JNevill Feb 18 '16 at 18:12
  • my question is - do I need to then make a tee or >> reference for each command in the shell script ? if I 20 commands, then each command has to have a tee or >> ? – hypermails Feb 18 '16 at 18:14
  • Are you looking for something like http://stackoverflow.com/questions/20355264/how-to-redirect-output-of-multiple-commands-to-one-file maybe? – Benjamin W. Feb 18 '16 at 18:23
  • Yeah... I mean, that's pretty simple, right? `command >> my.log` or `command | tee -a my.log` or `command` depending on your needs. I suppose you could write a function that takes in the stdout and a boolean whether you want to log it or not, but that seems even more cumbersome. – JNevill Feb 18 '16 at 18:24
  • I am trying to do this inside the script.. and not have to expect my users to do the tee or >> when they run. right now the best I have is "exec &> >(tee $0.log)" as my first line. – hypermails Feb 18 '16 at 18:45

2 Answers2

0

When you do not want to tell with each command who you like it logged, decide what you want to use most of the time. Is it tee? Try this:

function doit {
   echo "Normal (both)"
   # Some more commands without explicit rediection for both strout and logfile
   echo "only stdout" >&3
   echo "Only logfile" >> $0.log
}
exec 3>&1
echo " === Screen output"
doit | tee -a $0.log
echo " === Content file"
cat $0.log

Output

=== Screen output
only stdout
Normal (both)
 === Content file
Only logfile
Normal (both)
Walter A
  • 19,067
  • 2
  • 23
  • 43
0

thank you for your responses. after a lot of additional research - I decided to pick portions from the answer at How do I write stderr to a file while using "tee" with a pipe? - the post from Anthony.

Community
  • 1
  • 1
hypermails
  • 726
  • 1
  • 10
  • 28