Results 1 to 4 of 4
i'm trying to write a script that searches a given directory for music and plays it through another application serially.
in the playlist making section of the code, i run ...
- 04-25-2008 #1Just Joined!
- Join Date
- May 2007
- Posts
- 3
whitespace and 'for'
i'm trying to write a script that searches a given directory for music and plays it through another application serially.
in the playlist making section of the code, i run into problems. It seems the 'for' operator separates words from a name with whitespace as different items. I am not entirely sure if the problem is with 'for', but i believe that i might be able to circumvent this problem by having less piped through a program that finds whitespace in a name and inserts a '\' in front of it.
check_dir is another function that changes $DIR appropriately if there are subfolders.Code:function make_playlist { touch $PLAYLIST for i in $( ls $DIR ); do check_dir echo "$DIR$i" >> $PLAYLIST done } # end of make_playlist
does anyone know what I'm doing wrong?
- 04-25-2008 #2
Yes, for assumes that the list is delimited by whitespace.
You will have to change the IFS (Internal Field Separator).
Read This
Men occasionally stumble over the truth,
but most of them pick themselves up
and hurry off as if nothing had happened.
Winston Churchill
... then the Unix-Gods created "man" ...
- 04-26-2008 #3Just Joined!
- Join Date
- Apr 2008
- Posts
- 35
Even though it's old-style and some versions of bash have bug related to scope, you can always try this (although the above reply is better form):
change
toCode:for i in $( ls $DIR ); do
while will read the entire line as one variable unless you supply it with multiples.Code:$( ls $DIR )|while read i; do
Best wishes,
Mike
- 04-27-2008 #4Linux Guru
- Join Date
- Nov 2007
- Location
- Córdoba (Spain)
- Posts
- 1,513
First: never ever use ls as an input source, overall if ls is coloured. ANSI scape sequences for colors just add problems. Even more, in this case, it's just superfluous. What you are doing with this line:
Could be done with just:Code:for i in $( ls $DIR ); do
To be on the safe side, I'd also change this line:Code:for i in $DIR/*; do
By this one:Code:echo "$DIR$i" >> $PLAYLIST
Code:echo "${DIR}/${i}" >> $PLAYLIST


Reply With Quote
