Wednesday, December 18, 2013

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 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[@]}"
  : # ...
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.

No comments: