[Gllug] spaces and shell scripting

Jon Dye jon at pecorous.co.uk
Thu Sep 1 13:43:50 UTC 2005


Hi,

I'm trying to write a shell (bash) script that uses find to get a list 
of file names and do different things depending on whether there are 0, 
1 or more items in the list.

My problem is spaces in filenames and how to deal with them when I have 
a list of file names in a variable.  I got the script to work by running 
find multiple times and not using variables, e.g.

FILE_COUNT="find /some/directory -print | wc | cut -c 1"
if [ $FILE_COUNT -eq 0 ]; then
	# do something
elif [ $FILE_COUNT -eq 1 ]; then
	# do something else
	FILE=`find /some/directory -print`
	some_command "$FILE"
else
	# do something with the list of files
	find /some/directory -print0 | xargs -o some_other_command
fi

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.

Thanks,

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




More information about the GLLUG mailing list