Peter Smit
Peter Smit

Reputation: 28696

Why does this bash script not work as I want

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

Upvotes: 0

Views: 98

Answers (1)

Benoit
Benoit

Reputation: 79155

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

Upvotes: 2

Related Questions