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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-15-2009, 04:51 PM
|
#1
|
|
Member
Registered: Nov 2006
Posts: 86
Rep:
|
list filenames with spaces in a shell script
I am trying to iterate on all files in the current directory, using a bash script:
Code:
LSFILELIST=`ls -1`
for EACHFILE in "${LSFILELIST}"; do
echo "${EACHFILE}"
done
The problem: ${EACHFILE} "spits" the entire list of files, thinking it is one long filename (with spaces).
Trying to troubleshoot the problem, I came to the conclusion that the difficulty in using the for loop in such manner lies in the fact that I can current use $LSFILELIST in either:
1) bare (no quotes) $LSFILELIST:
Quote:
|
==> $LSFILELIST="file name1 file name2 file name3"
|
2) quoted "${LSFILELIST}"
Quote:
==> $LSFILELIST="file name1
file name2
file name3"
|
Neither of the above satisfies bash's 'for'!
What I really need instead is some mechanism that will generate for me $LSFILELIST like this:
Quote:
$LSFILELIST="file name1"
"file name2"
"file name3"
|
However, I don't know to accomplish this.
Clues, tips, hints or ideas would be greatly appreciated.
Thanks!
|
|
|
|
03-15-2009, 05:03 PM
|
#2
|
|
Guru
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678
Rep: 
|
This will find them for you
Code:
find ./ -type f -name '* *'
|
|
|
|
03-15-2009, 06:14 PM
|
#3
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
Two way of looping over file names from "ls" command, handling files names with spaces correctly: - Pipe output of "ls" directly to loop. Disadvantage is that if some variable is assigned to or changed inside the loop, the value will be lost after the loop. (TESTVAR here used to show this effect).
Code:
#!/bin/bash
TESTVAR="Initial test value"
ls -1 | while read FILE; do
echo "== FIRST LINE OF: $FILE"
head -1 "$FILE" # double quotes needed for filename with spaces!
echo
TESTVAR="Changed!"
done
echo "Value of TESTVAR after te loop = \"$TESTVAR\""
exit 0
- One way to solve the issue with changes to a variable inside the loop it to save the output of ls to a temp file.
Code:
#!/bin/bash
TESTVAR="Initial test value"
TMP=`mktemp` # Create a temp file, path stored in var $TMP
ls -1 >"$TMP" # Write list of files to temp file
while read FILE; do
echo "== FIRST LINE OF: $FILE"
head -1 "$FILE" # double quotes needed for filename with spaces!
echo
TESTVAR="Changed!"
done <$TMP # Feed the loop from temp file
echo "Value of TESTVAR after te loop = \"$TESTVAR\""
rm $TMP # When done with the file list, clean up temp file
exit 0
Note: When using a file name from a variable ($FILE in the examples) in a command, you should double-quote it to have it work with file names with spaces in them.
|
|
|
|
03-15-2009, 06:35 PM
|
#4
|
|
Member
Registered: Nov 2006
Posts: 86
Original Poster
Rep:
|
Hko, you seem to have found a solution to my problem although I may have not explained it very well: I don't need to read the files - I only need to have their filenames for various operations.
Thus, the following script is enough and seems to work:
Code:
#!/bin/bash
ls -1 | while read FILE; do
echo "$FILE" # double quotes needed for filename with spaces!
done
exit 0
However, I am not sure what the 'read' above does: Does it really open the file and attempts to read from it?
If so, then this may not be very efficient. I just want to be able to check whether the file exists in another directory - I don't need to read it or even open it.
Is there a way to accomplish the above without the 'read'?
Thank you!
|
|
|
|
03-15-2009, 07:07 PM
|
#5
|
|
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530
Rep: 
|
Quote:
Originally Posted by xp_newbie
Code:
#!/bin/bash
ls -1 | while read FILE; do
echo "$FILE" # double quotes needed for filename with spaces!
done
exit 0
However, I am not sure what the 'read' above does: Does it really open the file and attempts to read from it?
|
No. it does not read or open the files.
while read SOME_VAR; do just loops over each line it get from the standard input ("stdin"), assigning the line read to $SOME_VAR each time.
If you use it this way:
Code:
while read SOME_VAR; do
echo "==> $SOME_VAR"
done
This script will just wait for you to enter some lines until you press ctrl-d to signal "end of file (EOF)". The loop will echo each line you typed with "==>" before each line.
If you pipe the contents of a file with "cat" to the standard input of such a while-read-loop:
Code:
cat /path/to/some/file.txt | while read LINE; do
echo "==> $LINE"
done
Then "cat" reads the file and feeds it to the standard input (stdin) of the while-read-loop. So, the loop runs for each line in the file.
What I did in the post above is feed the output of "ls -1" to the while-read-loop the same way.
You said you need the list of file names "for various operations". If you need to loop more than one time over the same list of files, I'd recommend using the second example I posted (storing the list in a temp-file).
It is probably possible to do without a read-loop and do it with a for-loop instead. But I don't know one from the top of my head. Would need to fiddle with file seperators and some extra comands I suppose. But IMO there's no need to explicitly using a for-loop, and it would be probably be less efficient than while-read especially when the list of files is long.
Last edited by Hko; 03-15-2009 at 07:09 PM.
|
|
|
|
03-15-2009, 07:34 PM
|
#6
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,589
|
I've always found spaces to be a big headache too, and things like quoting variables and whatnot often don't work very well. I've found that the least troublesome way to handle spaces is to change the internal field separator in bas so that it doesn't recognize them.
Simply add the following to your script, or run them in the terminal:
Code:
OLDIFS=$IFS
IFS="
" #this is a line break
{the block of commands you want to run}
IFS=$OLDIFS
Now if everything goes as planned, spaces will be ignored in filenames.
Note that it isn't really necessary to back up and reset the IFS in a script, since variables set in scripts are only local in scope.
The default IFS is space, tab, and newline, BTW.
|
|
|
|
03-15-2009, 07:46 PM
|
#7
|
|
Member
Registered: Nov 2006
Posts: 86
Original Poster
Rep:
|
Quote:
Originally Posted by Hko
If you need to loop more than one time over the same list of files, I'd recommend using the second example I posted (storing the list in a temp-file).
|
Thank you very much for your great help. No, I don't need to loop more than once, so your original solution is 100% PERFECT for me. Thank you! 
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:23 PM.
|
|
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
|
|