[plug] Simple bash scripting problem

Richard Meyer meyerri at westnet.com.au
Mon Oct 10 18:29:15 WST 2005


On Mon, 2005-10-10 at 17:56 +0800, Richard Meyer wrote:
> 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 ...

GOT IT..................................

ls output


> richard1 at Selous:~/project> ls -l *.c
> -rw-r--r--  1 richard1 users    0 2005-10-10 18:04 fred d.c     <===== note spaces
> -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

shell prog 

> richard1 at Selous:~/project> cat test.sh
> #!/bin/bash
> echo $@
> for file in "$@"; do
>         find . -name "$file" -print
> done

output


> richard1 at Selous:~/project> find . -name "*.c" -print
> ./kids.c
> ./fred d.c
> ./prob9_1.c
> ./prob1.c
> ./prob2.c
> ./prob3.c
> ./prob4.c
> ./prob5.c
> ./prob6.c
> ./prob7.c
> ./prob8.c

OK, the order is different, but what the heck - exercise for the student
is to change the find command to do what you want ....

-- 
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