[Gllug] spaces and shell scripting

Peter Grandi pg_gllug at gllug.for.sabi.co.UK
Thu Sep 1 17:18:44 UTC 2005


>>> On Thu, 1 Sep 2005 18:04:16 +0100, pg_gllug at gllug.for.sabi.co.UK
>>> (Peter Grandi) said:

[ ... the dreaded 3-case filelist ... ]

pg_gllug> * Alternatively, one can, as some people suggested,
pg_gllug>   store the whole file list returned in the
pg_gllug>   ''lookeahead'' scan, obviously in a file [ ... ]

As someone pointed out, '-print' in 'find' does not allow
handling correctly the case where there are newlines in
pathnames. It is relatively easy (except for counting the number
of null separated records) to fix this...

  #!/bin/sh

  ME="`basename \"$0\"`"

  LIST="`mktemp`"; trap "rm -f '$LIST'" 0

  C="`find ${1+\"$@\"} -type f -print0 \
      | tee \"$LIST\" | tr -cd '\0' | wc -c`"

  case "$C" in
  '0') doNoElements.sh;;
  '1') : '1 file, scan again to get the name of that file'
       xargs -0 doSingleElement.sh;;
  *)   : 'N files, scan again to get the list'
       xargs -0 doOneOfManyElements.sh;;
  esac < "$LIST"

Similar 'xargs -0' logic can be applied to the previous
examples.

-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list