[plug] Simple bash scripting problem

Richard Meyer meyerri at westnet.com.au
Mon Oct 10 17:56:45 WST 2005


On Mon, 2005-10-10 at 17:49 +0800, Richard Meyer wrote:
> On Mon, 2005-10-10 at 17:36 +0800, Timothy White wrote:
> > Ok. So this should be easy. I don't want to write this script in perl,
> > I have most of it in bash already so I would like a nice simple
> > answer. (Google's not being my friend, probably cause I'm tired)
> > 
> > This script basically does a number of operations on a list of files.
> > The problem is getting the list to work.
> > 
> > I call the script similar to this "script *.flac" and what it to
> > obviously work on all the flac files... bash expands the *.flac to a
> > list of files. Unfortunately all the files have spaces in the names...
> > So the 'for list in word' part splits it on the spaces! What is the
> > easy solution?
> > 
> > Thanks
> > 
> > Tim
> > [1] The problem part....
> > for file in $*; do
> > 
> > if test -f $file; then
> 
> You could try 
> 
> > #!/bin/bash
> > for file in $1; do
> >         ls -l $file
> > done
>  
> and call it as below - with results ---
> 
> > richard1 at Selous:~/project> sh test.sh "*.c"
> > -rw-r--r--  1 richard1 users 2956 2005-07-29 12:02 kids.c
> > -rw-r--r--  1 richard1 users 51 2005-07-16 10:59 prob1.c
> > -rw-r--r--  1 richard1 users 331 2005-07-16 10:59 prob2.c
> > -rw-r--r--  1 richard1 users 209 2005-07-16 10:59 prob3.c
> > -rw-r--r--  1 richard1 users 324 2005-07-16 10:59 prob4.c
> > -rw-r--r--  1 richard1 users 165 2005-07-16 10:59 prob5.c
> > -rw-r--r--  1 richard1 users 141 2005-07-16 10:59 prob6.c
> > -rw-r--r--  1 richard1 users 156 2005-07-16 10:59 prob7.c
> > -rw-r--r--  1 richard1 users 362 2005-07-16 14:17 prob8.c
> > -rw-r--r--  1 richard1 users 749 2005-07-16 14:47 prob9_1.c
> 
> OK, I don't have spaces, but it IS doing the expansion in the script, not the command line ..
> 
> HTH

Sorry, just created a file with spaces in the name and it don't work -
it gives 


> richard1 at Selous:~/project> sh test.sh "*.c"
> ls: fred: No such file or directory
> ls: d.c: No such file or directory

when 

> richard1 at Selous:~/project> ls -l fred*
> -rw-r--r--  1 richard1 users 0 2005-10-10 17:51 fred d.c

Umm, now I'll have to think on this a bit more ..

So is it doing the expansion there?

+++++++++++++++++++++++++++++++++++++

No I lied - I'll have to experiment ...
-- 
Richard Meyer <meyerri at westnet.com.au>




More information about the plug mailing list