[geeks] ksh for loop question

Greg A. Woods woods at weird.com
Fri May 10 12:17:17 CDT 2002


[ On Friday, May 10, 2002 at 11:54:33 (-0400), Brian Hechinger wrote: ]
> Subject: [geeks] ksh for loop question
>
> ok, i have a list of files that unfortunately have spaces in them.  i'd like
> to get this list of files from a server using wget, so i was looking to just
> use a for loop to go and get them all, for example:
> 
> for files in `cat file_list.txt`; do
>   wget "http://www.server.com/$files"
> done
> 
> this of course fails misserably since for splits by whitespace.  how do i do
> something like this, but split by line not by whitespace?  and please don't
> say perl.

	oIFS="$IFS"
	IFS=""
	while read -r filename; do
		wget "http://www.server.com/$filename"
	done < file_list.txt
	IFS="$oIFS"
	unset oIFS files

Strictly speaking you should not even need to nullify IFS because if
there are more fields than parameters, the last parameter is assigned
all the remaining fields (inclusive of any separating whitespace).

The above should work just fine in any POSIX shell actually, not just ksh.

(Note that you don't include a newline in IFS because 'read' doesn't
split its string until after the newline has been stripped from the line
that was read.)

-- 
								Greg A. Woods

+1 416 218-0098;  <gwoods at acm.org>;  <g.a.woods at ieee.org>;  <woods at robohack.ca>
Planix, Inc. <woods at planix.com>; VE3TCP; Secrets of the Weird <woods at weird.com>



More information about the geeks mailing list