Batch Command with Output Redirection – Bash Scripting Guide

bashscripts

When I want to process a bunch of tex files, I use

for f in *.tex; do latex "$f" ; done;

What do I do when I need to redirect output to another file, like in case of catdvi?

The following does not work:

for f in *.dvi ; do catdvi -e 1 -U $f  > "${f%.dvi}.txt"; done;

What am I doing wrong?

Best Answer

You forgot to quote $f, so the filename probably got wordsplit due to whitespace in the filename. I also suggest using ./*.dvi as this avoid problems if a filename starts with a - (which will make most commands treat it as a set of options instead of a filename)

for f in ./*.dvi; do catdvi -e 1 -U "$f" > "${f%.dvi}.txt"; done

Or perhaps combine the two loops

for f in ./*.tex; do 
    base=${f%.tex};
    latex "$f" &&
    catdvi -e 1 -U "$base.dvi" > "$base.txt";
done