Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: How to iterate over lines cross-platform
- X-seq: zsh-users 15001
- From: Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx>
- To: zsh-users@xxxxxxx
- Subject: Re: How to iterate over lines cross-platform
- Date: Sat, 10 Apr 2010 13:05:24 -0700
- In-reply-to: <hpqcvj$avi$1@xxxxxxxxxxxxxxx>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-help@xxxxxxx; run by ezmlm
- References: <hpqcvj$avi$1@xxxxxxxxxxxxxxx>
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