Using dos2unix and attaching it to a while loop

Williams, Gerald S (Jerry)
Mon Apr 9 15:00:00 GMT 2007 wrote:
> I am dealing with DOS text files and need to output DOS text files.
> I found dos2unix, but I do not know how to properly implement it. The
> following Bash code is a work-in- progress. Please let me know if a
> efficient approach exists. 
> while read line
> 	do
> 		i=0
> 		echo "LINE -> $line"
> 		echo "i -> $i"
> 		MP3[i++]=$(awk '/^.*\.mp3/ { print $1}')
> 		CRC[i++]=$(awk '/^.*\.mp3/ { print $3}')
> 		echo "MP3[i] -> $MP3[i]"
> 		echo "CRC[i] -> $CRC[i]"
> 	done < <(dos2unix "$FILE")
>  #do some stuff  then
> unix2dos "$FILE"
> The cygwin console returns the following error. My syntax is not
> line 132: syntax error near unexpected token `<'
> line 132: `          done < <
> (dos2unix "$FILE")'

There are many problems with that code snippet. I'll start by answering
your question:


Yes, that format is incorrect. I think perhaps you are confusing the
$(...) operator, although that's not really what you want either.

"dos2unix FILE" converts the file in place, so to use that form you
would simply do this:

 dos2unix "$FILE"
 while read line
 done < "$FILE"
 unix2dos "$FILE"

If you want convert the contents of "$FILE" without changing the file
in place, you could use dos2unix as a filter as follows:

 dos2unix < "$FILE" | while read line


I also noticed that i is reset to zero on each pass, then incremented
in a presumably incorrect manner. This code sets MP3[0] and CRC[1] on
each pass, then tries to print ${MP3[2]} and ${CRC[2]}. I'm reasonably
certain that you also don't want to run awk quite the way you're doing
it--perhaps you meant to do something like $(echo "$line" | awk ...)?
Finally, "$MP3[i]" doesn't access array elements, nor does it expand
the value of i--I think you meant "${MP3[$i]}". But as you said it is
a work in progress...



Disclaimer: I rarely use BASH myself, although I do quite a bit with
ZSH (because I can), KSH (because I have to), and SH (because we all
ought to). :-)

Unsubscribe info:
Problem reports:

More information about the Cygwin mailing list