[Gllug] redirecting stdout to multiple files

- Tethys tethys at gmail.com
Fri Dec 15 16:21:23 UTC 2006


On 12/15/06, Ben Fitzgerald <ben_m_f at yahoo.co.uk> wrote:

> I want to be able to have a single line that outputs to stdout and a
> logfile without spawning a subshell by using a pipe.

Spawning processes under Linux is cheap.

> 1. output to stdout only
> 2. output to stdout and logfile
>
> I +could+ just use tee, it's true but then I'd need two lines, AFAIK:
>
> [ -n "$stdout_only" ] && echo hi
> [ -z "$stdout_only" ] && echo hi | tee -a /tmp/mylog.txt

So why not once (at the start of the script) do:

  logfile=/tmp/mylog.txt
  [ -z "$stdout_only" ] || logfile=/dev/null

Then you can later do:

  echo hi | tee -a "$logfile"

as many times as you like, and it'll do the right thing.

Tet
-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list