Find the answer to your Linux question:
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 ...
  1. #1
    Just 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.

    Code:
    function make_playlist
    {
    	touch $PLAYLIST
    	for i in $( ls $DIR ); do
            check_dir
    	echo "$DIR$i" >> $PLAYLIST
    	done
    }	# end of make_playlist
    check_dir is another function that changes $DIR appropriately if there are subfolders.

    does anyone know what I'm doing wrong?

  2. #2
    Linux User dxqcanada's Avatar
    Join Date
    Sep 2006
    Location
    Canada
    Posts
    259
    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" ...

  3. #3
    Just 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

    Code:
    for i in $( ls $DIR ); do
    to

    Code:
    $( ls $DIR )|while read i; do
    while will read the entire line as one variable unless you supply it with multiples.

    Best wishes,

    Mike

  4. #4
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    Quote Originally Posted by DorianEzra View Post
    Code:
    function make_playlist
    {
    	touch $PLAYLIST
    	for i in $( ls $DIR ); do
            check_dir
    	echo "$DIR$i" >> $PLAYLIST
    	done
    }	# end of make_playlist
    check_dir is another function that changes $DIR appropriately if there are subfolders.

    does anyone know what I'm doing wrong?
    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:

    Code:
    	for i in $( ls $DIR ); do
    Could be done with just:

    Code:
    	for i in $DIR/*; do
    To be on the safe side, I'd also change this line:

    Code:
    	echo "$DIR$i" >> $PLAYLIST
    By this one:

    Code:
    	echo "${DIR}/${i}" >> $PLAYLIST

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...