[plug] Simple bash scripting problem

Richard Meyer meyerri at westnet.com.au
Mon Oct 10 17:49:59 WST 2005


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
-- 
Richard Meyer <meyerri at westnet.com.au>
Cutting the space budget really restores my faith in humanity.  It
eliminates dreams, goals, and ideals and lets us get straight to the
business of hate, debauchery, and self-annihilation.
                -- Johnny Hart

Linux Counter user #306629




More information about the plug mailing list