[clue-tech] Error messages that can't be redirected

marcus hall marcus at tuells.org
Fri Nov 6 22:09:16 MST 2009


On Fri, Nov 6, 2009 at 9:46 PM, Matt Gushee <matt at gushee.net> wrote:

> Hi folks--
>
> Here's something that's been bugging me for a long time: sometimes I will
> use a command-line program and try to redirect the output, e.g. to a log
> file, and it doesn't work. For example:
>
>  for img in *.jpg; do
>    nice gm convert -resize 160x160 $img thumbs/$img >> resize.log 2>&1
>  done
>
> Nope. I get nothing in my log file. I've tried several variations of this
> command, including piping the output to tee. It just doesn't ever work.
> 'gm', by the way, is GraphicsMagick, a better-performing fork of
> ImageMagick. The same thing happened with ImageMagick, and IIRC with some
> other programs I've used on occasion.
>
> So what's going on here?
>
> --
> Matt Gushee

When I run convert with these parameters, I get no output.  Normally,
silence is good, so just what do you expect to get as output?

Just for reference, it is possible to do the re-direction on the whole
loop like this:

for img in *.jpg; do
   nice gm convert -resize 160x160 $img thumbs/$img
done > resize.log 2>&1

You can either write or append to the log file as appropriate (depending
on whether you want to keep any previous contents).

On Fri, Nov 06, 2009 at 09:50:39PM -0700, Silas Martinez wrote:
> I'm assuming you've tried
> for img in *.jpg; do
>    nice gm convert -resize 160x160 $img thumbs/$img 2>&1 >> resize.log
> done

No, putting the "2>&1" before the *file* re-direction will first send
stderr output to where stdout is pointing, *then* redirect stdout to
resize.log.  The way Matt wrote it will first re-direct stdout to the
file and then direct stderr to the same place.

If you're dealing with a pipe you have to write the "2>&1" before the pipe
because the shell sets up pipes before processing the arguments, so in
a pipeline when the shell starts scanning arguments stdout is already
hooked into the pipe (and anything following the pipe will be part of
the next element in the pipeline!).  But, file re-direction is processed
during argument scanning, so stdout needs to be sent to the file before
sending stderr to &1.

Marcus Hall
marcus at tuells.org


More information about the clue-tech mailing list