tags:

views:

42

answers:

1

I have the following bash script

for s in $(ls -1 fig/*.py); do
   name=`basename $s .py`
   if [ -e "fig/$name.pdf" -o "fig/$name.pdf" -ot "fig/$name.data" -ot "fig/$name.py" ]; then
      $s
   fi
done

It is supposed to invoke a python script if the output pdf does not exist, or the pdf is older than the py or data file.

Unfortunaly, the script is now never invoked. What did I do wrong?

EDIT

Thanks Benoit! My final script is:

for s in fig/*.py ; do # */ fix highlighting
    name="$(basename "$s" .py)"
    if test ! -e "fig/$name.pdf" -o "fig/$name.pdf" -ot "fig/$name.data" -o "fig/$name.pdf" -ot "fig/$name.py"
    then
        "$s"
    fi
done
+2  A: 

Many mistakes. See bash pitfalls especially first one (never rely on ls to get a list of files).

It also seems that the test is not well-formed: Two -ot in a row seem strange to me. Using -o to perform an OR instead of an AND seems weird also.

for s in fig/*.py ; do           # */ for code colouring on SO
    name="$(basename "$s" .py)"
    if test -e "fig/$name.pdf" -o "fig/$name.pdf" -ot "fig/$name.data" -ot "fig/$name.py"
    then
        "$s"
    fi
done
Benoit
Sorry, at first I copied things wrong from my editor. Could you update your answer?
Peter Smit
Thanks!! I see that I really messed up a bit with the or operators and so. I should rewrite it myself to negate the -e operator and to add an -o between the to -o statements
Peter Smit