Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I tried to put the output of my program into a text file. It appends the echo commands to the file properly, but the imagemagick "compare" command is not appended to the file. It just STOUD's the "PSNR value", which is returned by compare method into terminal.

Is there a way to append this commands output to the textfile too? Also if I call my script just with "./script.sh > test.txt" it doesn't print anything more than the echos to file and compare results to terminal.

Here's a part of my code:

ls images/toconvert/ > lsout.txt

while read LINE
    echo ====================== $LINE ==================== >> psnrdiff.txt
    echo Jpeg2000 >>  psnrdiff.txt
    compare -metric PSNR images/toconvert/$LINE images/converted/$LINE.jp2 images/psnrDiffs/$LINE.jp2.png >> psnrdiff.txt
done < lsout.txt
share|improve this question
Your script is overly complicated and will break on some “weird” file names. Write for x in images/toconvert/*; do … compare -metric PSNR "$x" "images/converted/${x##*/}.jp2" "images/converted/${x##*/}.jp2.png"; done – Gilles Apr 26 '14 at 23:23
i differ between three types .. i mentioned before it's just a "part" of my code and not even this part is in the current code form, but thanks – Stefan Sprenger Apr 28 '14 at 11:59
up vote 3 down vote accepted

Various imagemagick command output to STDERR instead of STDOUT.

You can redirect STDERR to STDOUT to capture the output:

compare -metric PSNR .... >> psnrdiff.txt 2>&1
share|improve this answer
ls images/toconvert/ > file.txt 
while read LINE
    echo ====================== $LINE ==================== >> psnrdiff.txt
    echo Jpeg2000 >>  psnrdiff.txt
    compare -metric PSNR "images/toconvert/$LINE" "images/converted/$LINE.jp2" "images/psnrDiffs/$LINE.jp2.png" >> psnrdiff.txt
done < file.txt
share|improve this answer
keeps printing it into console, but just the compare command return value is printed – Stefan Sprenger Apr 26 '14 at 11:50
Try the command suggested by Gnouc in above piece of code. – Mandar Shinde Apr 26 '14 at 11:53
when i have enough rep i will vote you up .. thx – Stefan Sprenger Apr 26 '14 at 11:58
My pleasure :-) – Mandar Shinde Apr 26 '14 at 11:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.