Advertisement
kipkoan

Changing Log Output

Feb 20th, 2012
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.53 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. PROGNAME=$(basename $(readlink -f $0))
  4. LOG="output-$PROGNAME.log"
  5.  
  6. _output_echo () {
  7.   while read data; do
  8.     echo -e "$PROGNAME: $data"
  9.     echo -e $data | _output_file
  10.   done
  11. }
  12.  
  13. _output_file () {
  14.   while read data; do
  15.     echo -e "$PROGNAME: $data" >> $LOG
  16.   done
  17. }
  18.  
  19. # program output to log file and optionally echo to screen (if $1 is "-e")
  20. log () {
  21.   if [ "$1" = '-e' ]; then
  22.     shift
  23.     $@ | _output_echo
  24.   else
  25.     $@ | _output_file
  26.   fi
  27. }
  28.  
  29. log echo "Log File Only"
  30. log -e echo "Echo & Log File"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement