[Gllug] spaces and shell scripting
Tethys
sta296 at astradyne.co.uk
Thu Sep 1 14:13:15 UTC 2005
Jon Dye writes:
>I'm trying to write a shell (bash) script
Try and avoid writing bash scripts. Unless you really need a bash
specific feature, write a generic bourne shell script (or korn shell
if you need more than /bin/sh provides).
>but I want to avoid multiple find statements because they are slow. I
>guess I want either to put the list of files in a variable with
>delimiters between the file names or put them into an array somehow.
>
>If any can tell me how to achieve what I want (or suggest a better way
>to do it) I'd be grateful.
Put the output of the find command into a temporary file, and work
on it there. This will handle files containing whitespace and other
metacharacters, provided you don't have filenames containing newlines[1].
tmpfile=/tmp/files.$$
trap "/bin/rm -f $tmpfile" 0 HUP INT QUIT TERM
find /some/directory -type f -print > "$tmpfile"
c=$(wc -l /tmp/files | cut -d' ' -f1)
case $c in
0) do_somthing ;;
1) do_something_else "$(cat $tmpfile)" ;;
*)
while read file
do
some_other_command "$file"
done < "$tmpfile"
esac
Tet
[1] Not a common occurrence... unless they're somewhere that's mounted
by a Windows or Mac box, at which point it becomes more likely.
--
Gllug mailing list - Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug
More information about the GLLUG
mailing list