shell code and useless use of “ls *.tar”

Consider this code (I came across such Korn shell script code recently):

TARFILE=$(ls -1 /tmp/*.tar)

According to http://partmaps.org/era/unix/award.html#ls you ought to replace the ls with a glob, e.g. like this:

[[ -s /tmp/*.tar ]] && set -A tarfiles /tmp/*.tar
for TARFILE in “${tarfiles[@]}”
do
  : # …
done

Instead you may want to enquire ${#tarfiles[@]}, i.e. the number of files globbed.

Please forgive me the naming here! I was happy to take this note anyway.


Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.