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

Re: How to iterate over lines cross-platform



On Apr 10,  7:36pm, Thorsten Kampe wrote:
}
} How can I iterate over lines of output when I don't know in advance 
} whether the line endings will be DOS or Unix?!

You might have better luck if you do not change IFS from the default,
and use this:

slptool findscopes |
while read scope; do
    slptool -s "$scope" findsrvtypes |
    while read srvtype; do
        [inner loop]
    done
done

It should be the case that "read" does the right thing with line ends.

} The reason is that the slptool on Windows is a native Win32 
} application that outputs lines with "\r\n" endings (and not "\n").

I believe this means that "$scope" has a trailing "\r" and therefore
slptool -s "$scope" doesn't find a matching scope.  If it turns out
that "read" also mishandles things, use ${scope%$'\r'} to trim.
 
} So I simply set "IFS=$'\r\n'" and now the output is correct - but the 
} scipt goes through an additional iteration for each loop

Yes, as you suspected, IFS=$'\r\n' means that each of "\r" and "\n" is
individually taken as a separator, so "foo\r\n" splits into "foo" and
(empty string).



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