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

Re: Bash question



ls | while read file
do
	cat "$bob"
done

for loops treat spaces and newlines the same (which is what you are
seeing), the read command reads till the end of the line including
spaces. Note that when you use the environment variable you must quote
it (be sure to use " as opposed to ').

If you want to look at a man page 'man bash' -- it's a builtin.

rgh

On Tue, 2003-07-01 at 17:46, Stephan Sauerburger wrote:
> This is a general shell-scripting question:
> 
> In a for loop which runs through all files, as in:
> 
> for file in `ls`
> do
>   #stuff
> done
> 
> 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`
> do
>   mpg123 $file
> done
> 
> ...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?
> 
> Thx,
> 
> -- 
> Stephan
-- 
"It is possible to make things of great complexity out of things
 that are very simple. There is no conservation of simplicity"
 -- Stephen Wolfram

Richard Heycock <rgh@roughage.com.au>
tel : 0410 646 369
key fingerprint : 909D CBFA C669 AC2F A937  AFA4 661B 9D21 EAAB 4291



Reply to: