开发者

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开发者_JAVA百科 "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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜