[Gllug] Re: spaces and shell scripting

Jon Dye jon at pecorous.co.uk
Thu Sep 1 14:26:33 UTC 2005


Hi Liam,

Liam Smit wrote:
>>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.
> 
> Why put a list of file names in a variable? 

I want to find files based on some criteria and then depending on how 
many I've found I want to run different commands on them.  If there are 
no files then I want to do something to the root directory, if there is 
one then I want to pass it as an argument to a program if there are 
multiple files I want to pass them as arguments to a different program.

I didn't want to do the find operation more than once because it's slow. 
  I thought storing the result of the find in a variable might be the 
answer, if you can think of a better way then it'd be gratefully received.

I've just realised it might be easier to put it in a temporary file so I 
might give that a go.

> It sounds like each filename should be in it's own variable.

How do I create an arbitrary number of variables depending on how many 
files I find?  Or are you suggesting an array?

>>FILE_COUNT="find /some/directory -print | wc | cut -c 1"
> 
> Perhaps use wc -l to count lines, just put each file name on a new line?

Oops, that is actually how I've done it, the example was done from 
(poor) memory and doesn't even work.

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




More information about the GLLUG mailing list