LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-14-2005, 01:01 PM   #1
Quantum0726
Member
 
Registered: Dec 2003
Location: Milwaukee, WI
Distribution: Gentoo
Posts: 39

Rep: Reputation: 15
bash script with spaces


I am running into one problem with a bash script when the files it's analyzing contain spaces in the name. After some debugging I've narrowed it down to the following. I have a variable that's formed by grep'ing multiple lines out of a file. The contents of the variable are the filenames separated by \n characters (at least that's what it looks like when piping grep into a file. For example:

file name 1.txt
program name 1.sh

Now I need to perform several operations on each file individually. If I use a for loop this should work nicely.

for F in $FILENAMES
...
...
done

This code breaks the files up by spaces and \n characters, so in this case I get 6 iterations. If I enclose the $FILENAMES variable in "s, then I get only one string to work with, the entire contents of $FILENAMES in one iteration. What I need to have is $F be 'file name 1.txt' in the first iteration and 'program name 1.sh' in the second iteration (without the 's of course). What's a good way for me to do this?

Thanks much!
 
Old 11-14-2005, 01:10 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
This is a very common problem.

Here's a link to a reponse I made: http://www.linuxquestions.org/questi...81#post1864781

Doing a search in the forums for similar words or phrases ought to give you a ton of other ways.
 
Old 11-14-2005, 09:26 PM   #3
Quantum0726
Member
 
Registered: Dec 2003
Location: Milwaukee, WI
Distribution: Gentoo
Posts: 39

Original Poster
Rep: Reputation: 15
Thanks, I've been looking for something like this for quite some time but always managed to just find a more convoluted way around in the past. Your hint works great. Only thing I did different is kept the same 'for' loop code (without the quotes) as I need the variable later on and no need to grep the file twice. After using the IFS trick it seems that quoting isn't nearly as touchy in for loops.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to remove capitalisation and spaces form a filename scuzzman Programming 11 05-18-2008 12:28 PM
Spaces in bash parameters rose_bud4201 Programming 5 03-11-2005 06:57 AM
Spaces in filenames with BASH edenning Programming 12 01-27-2005 07:10 AM
bash and filenames with spaces doctorwebbox Linux - General 2 12-15-2004 08:55 PM
bash script ? -- spaces in passed parameters azwr Linux - Newbie 3 06-18-2004 06:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration