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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
08-12-2005, 01:25 PM
|
#1
|
LQ Guru
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507
Rep: 
|
Shell Scripting and Files With Spaces
A little tip:
If you're writing a shell script that's going to use filenames that have spaces in them, and you need to iterate through them, you may find that bash splits the files in the wrong place. For example, if I have a file called "A file with spaces", bash may try to treat that as 4 files.
So, here's the trick... use the -Q option to ls. It automatically quotes your filenames for you.
Code:
#!/bin/bash
for file in `ls -Q`
do echo $file
done
|
|
|
08-12-2005, 09:49 PM
|
#2
|
Senior Member
Registered: May 2004
Location: Sebec, ME, USA
Distribution: Debian Etch, Windows XP Home, FreeBSD
Posts: 1,445
Rep:
|
i believe you can also write it as
treating the ' ' as a special character in the filename, the same way you would with $,etc
|
|
|
08-13-2005, 02:16 AM
|
#3
|
Member
Registered: Nov 2004
Location: Germany
Distribution: Debian Testing
Posts: 332
Rep:
|
You can, but that won't solve the problem with ls.
|
|
|
08-13-2005, 09:14 AM
|
#4
|
LQ Guru
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507
Original Poster
Rep: 
|
Yeah, this wasn't supposed to be "everything", but it was supposed to be an easy way when dealing with ls. 
|
|
|
08-13-2005, 12:50 PM
|
#5
|
Senior Member
Registered: May 2004
Location: Sebec, ME, USA
Distribution: Debian Etch, Windows XP Home, FreeBSD
Posts: 1,445
Rep:
|
oops, my bad. Sorry to be off topic 
|
|
|
08-13-2005, 02:01 PM
|
#6
|
LQ Guru
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507
Original Poster
Rep: 
|
microsoft/linux: no, it's cool, it was very much related, but it just addresses two separate needs. I just discovered the -Q and --quote-style options to ls and thought it was worth sharing, with all the troubles I've seen on here with filename splitting.
|
|
|
08-13-2005, 03:10 PM
|
#7
|
Member
Registered: May 2005
Posts: 378
Rep:
|
A neat trick indeed, but if it's GNU-specific your script won't be portable to UNIX systems. Better would be
Code:
#!/bin/sh
ls | while read name
do
echo "$name"
done
Last edited by eddiebaby1023; 08-13-2005 at 03:15 PM.
|
|
|
08-13-2005, 03:26 PM
|
#8
|
LQ Guru
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507
Original Poster
Rep: 
|
Running your script gives:
Code:
$ touch "This is a test file"
$ touch "This is also a test file"
$ ./script.sh
This is a test file
This is also a test file
script.sh
Running it with it modified to put backslashes in front of the quotes gives:
Code:
$ ./script.sh
"This is a test file"
"This is also a test file"
"script.sh"
Which is, surprisingly, intended behavior. My interpretation of the read man page was incorrect. 
|
|
|
08-14-2005, 08:07 AM
|
#9
|
Member
Registered: May 2005
Posts: 378
Rep:
|
I put quotes around the echo()ed argument to preserve the whitespace in the filenames. Try the two variations with more than a single space (or tabs) between the words in the filename and see the difference.
|
|
|
08-15-2005, 05:04 PM
|
#10
|
Member
Registered: Jan 2004
Location: Colombia
Distribution: Ubuntu 5.10
Posts: 35
Rep:
|
I find this topic doing my first ever script, and I still haven't found how to solve it, here is what I did, based on what you said:
Code:
for file in `ls -Q $1 | grep .m3u`
do
echo "$1""$file"
done
And this is the output:
Quote:
/home/julian/"how
/home/julian/to
/home/julian/dismantel
/home/julian/an
/home/julian/atomic
/home/julian/bomb.m3u"
/home/julian/"Tyrannosaurus
/home/julian/hives.m3u"
|
And the correct ouput will be
Quote:
/home/julian/"how to dismantel an atomic bomb.m3u"
/home/julian/"Tyrannosaurus hives.m3u
|
So what do you suggest??
Last edited by julian_s; 08-15-2005 at 05:15 PM.
|
|
|
08-15-2005, 06:21 PM
|
#11
|
Member
Registered: Jan 2004
Location: Colombia
Distribution: Ubuntu 5.10
Posts: 35
Rep:
|
Never mind, I found another way:
Code:
#!/bin/bash
OLDIFS="$IFS"
IFS='
'
for fic in $(ls); do
echo $fic
done
IFS="$OLDIFS"
Sweeeeeeet!!!!!!!!!!!!!!! 
|
|
|
08-16-2005, 06:16 AM
|
#12
|
Member
Registered: Jul 2004
Location: Santiago, Chile
Distribution: Ubuntu
Posts: 410
Rep:
|
I think that
Code:
for i in *.m3u; do ...
also works. Does't it?
|
|
|
08-17-2005, 01:43 AM
|
#13
|
LQ Guru
Registered: Nov 2004
Location: San Jose, CA
Distribution: Debian, Arch
Posts: 8,507
Original Poster
Rep: 
|
enemorales: try that with files containing spaces in the names?
|
|
|
All times are GMT -5. The time now is 01:01 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|