[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bash question

This is a general shell-scripting question:

In a for loop which runs through all files, as in:

for file in `ls`

How do I have it make sure it iterates file-by-file? The following example, to play all
mp3s in the current directory:

for file in `ls`
 mpg123 $file

...will do just a fine job, so long as none of the file names have any
spaces in them. If my directory contained:

Aerosmith - Walk This Way.mp3
(They Might Be Giants) - Istanbul.mp3
Pink Floyd - The Wall.mp3

Then the preceding instructions first try to play "Aerosmith", then
"-", then "Walk", then "This", then "Way.mp3", then "(They"... you get the
idea. All of which fail, and this could lead to dangerous results if you're
doing something other than mpg123 to it, like "rm", and there were also a
file simply called "Aerosmith".

So how can one have the for loop separate the elements of the list only by
newlines ("\n"), filling the contents of "file" with the whole line,
and not separate by spaces, tabs, or other white space?



Reply to: