[clug] Next in my series of "how to do things in /bin/sh that you probably shouldn't".

Andrew Janke a.janke at gmail.com
Wed Aug 19 20:44:42 MDT 2009


> There's another way, but it's "cheating" and will cost you an extra
> process.

As it turns out what you are suggesting is like what others (who
emailed privately) suggested regarding using a wrapper script for all
such scripts that you would like to do this with.

> And you have to pick an environment variable that won't
> clash (unless you want to muck around using eval to set an envvar
> based on $$ (in the parent) and $PPID (in the child)).

erky!  Surely the name of the script plus a bit of glook would work in
99.9% of cases.

>    #!/bin/bash
>    if [ "x$redir" = "x" ]; then
>        redir=y "$0" "$@" 2>&1 | tee ${0##*/}.log
>        exit $?
>    else
>        unset redir
>    fi

Nice! Still I am not entirely sure why you need the else as "$redir"
should be local to the call inside the if? Or am I missing something?

#! /bin/sh

if [ "x$redir" = "x" ]; then
   redir=y "$0" "$@" 2>&1 | tee fred.log
   exit $?
fi

echo "Fred - $@ - $*"

Works just as well with the added bonus that it can be done in /bin/sh
should you try to run this on OSX and get caught with the sh/bash/ash
default weirdness.


Thanks


--
Andrew Janke
(a.janke at gmail.com || http://a.janke.googlepages.com/)
Canberra->Australia    +61 (402) 700 883


More information about the linux mailing list