Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

How to iterate over lines cross-platform



Hi,

I wrote a Zsh shell script which iterates over the output of an external 
tool ("slptool"). Each line of output is an item so I did this:

###
IFS=$'\n'

for scope in $(slptool findscopes); do
    for srvtype in $(slptool -s "$scope" findsrvtypes); do
        [inner loop]
    done
done
###

This works fine on Linux. Unfortunately the script does not works under 
Cygwin. It never enters the "for srvtype" loop (or it doesn't produce 
output). The reason is that the slptool on Windows is a native Win32 
application that outputs lines with "\r\n" endings (and not "\n").

So I simply set "IFS=$'\r\n'" and now the output is correct - but the 
scipt goes through an additional iteration for each loop so output is 
duplicated or results in an error at the end. I suspect that the shell 
does not split on "\r\n" but either on "\r" or "\n".

How can I iterate over lines of output when I don't know in advance 
whether the line endings will be DOS or Unix?!


Thorsten



Messages sorted by: Reverse Date, Date, Thread, Author