LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 09-09-2002, 03:49 AM   #1
RefriedBean
Member
 
Registered: Jun 2002
Location: N 37° 33.327 E 126° 55.650
Distribution: Gentoo, Slackware, OpenZaurus
Posts: 186

Rep: Reputation: 31
Bash Scripting Question


Hi!
I'm working on a simple script that passes a filename stored in a file (each filename is stored on a seperate line), as an argument to a program.

It uses a for loop.

Code:
 
for i in $( cat ~/.list ); do
       program $i
done
I have a few files that contain spaces, like;

aaaaa\ bbb.jpg

The problem is, that 'for' takes that as two names, thus the program won't work on files with spaces.

Now my question;
How can I make it see a filename with spaces in as one name?

Thanks!
RefriedBean
 
Old 09-09-2002, 06:18 AM   #2
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
If you have quotes around the filenames with spaces in the .list file then you can easily use xargs to get it to run the command with each filename seperately. Something like:

cat ~/.list | xargs -n 1 programname

You can get ls to put quotes around each filename so you could do something like this.

ls --quoting-style=c *.jpg | xargs -n1 programname

That would run the program for each .jpg file in the current directory even if it had spaces in the name.
 
Old 09-09-2002, 07:21 AM   #3
no2nt
Member
 
Registered: Aug 2001
Location: South Carolina, USA
Distribution: Redhat 8.0/Custom
Posts: 96

Rep: Reputation: 16
this should do the job

Code:
for i in "$( cat ~/.list )"; 
do
    program "$i"
done

Last edited by no2nt; 09-09-2002 at 07:26 AM.
 
Old 09-09-2002, 07:44 AM   #4
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
no2nt your example would work for one file with spaces in it. But it won't call the program one time for each filename in the list file. It just sends the complete list of files as one argument and only calls the program once.
 
Old 09-09-2002, 10:19 AM   #5
no2nt
Member
 
Registered: Aug 2001
Location: South Carolina, USA
Distribution: Redhat 8.0/Custom
Posts: 96

Rep: Reputation: 16
oh, i'm extremely sorry. I left something out!

Code:
IFS=$'\n'  # we need to handle tabs and spaces properly
for i in $( cat ~/.list );
do
    program "$i"
done
unset IFS  # return the inter-field separator to normal
thanks for showing me my error of ommision :-)

Last edited by no2nt; 09-09-2002 at 10:22 AM.
 
Old 09-10-2002, 02:14 AM   #6
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
That IFS parameter is rather handy. I gotta remember that one.
 
Old 09-11-2002, 09:06 AM   #7
RefriedBean
Member
 
Registered: Jun 2002
Location: N 37° 33.327 E 126° 55.650
Distribution: Gentoo, Slackware, OpenZaurus
Posts: 186

Original Poster
Rep: Reputation: 31
Thank you for all for your replies!

RefriedBean
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Scripting Question fiod Linux - Newbie 4 11-19-2005 05:09 AM
Bash scripting question ajaye1971 Linux - Newbie 1 11-16-2005 07:29 PM
Bash scripting question. pete1234 Programming 23 09-22-2005 06:52 PM
bash scripting question mehesque Programming 2 03-07-2004 01:37 PM
Bash scripting question. welby Linux - Software 1 01-14-2004 10:05 AM

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

All times are GMT -5. The time now is 04:47 AM.

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