X-Recipient: archive-cygwin@delorie.com
X-SWARE-Spam-Status: No, hits=-7.5 required=5.0	tests=AWL,BAYES_00,RCVD_IN_DNSWL_HI,RP_MATCHES_RCVD,SPF_HELO_PASS
X-Spam-Check-By: sourceware.org
Message-ID: <4ED4F78D.303@redhat.com>
Date: Tue, 29 Nov 2011 08:17:33 -0700
From: Eric Blake <eblake@redhat.com>
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:8.0) Gecko/20111115 Thunderbird/8.0
MIME-Version: 1.0
To: cygwin@cygwin.com
Subject: Re: Shell script - is this expected behaviour?
References: <jb26vu$5tc$1@dough.gmane.org>
In-Reply-To: <jb26vu$5tc$1@dough.gmane.org>
OpenPGP: url=http://people.redhat.com/eblake/eblake.gpg
Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="------------enig9B70BF089CC72F62D102BF09"
X-IsSubscribed: yes
Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe@cygwin.com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin@cygwin.com>
List-Help: <mailto:cygwin-help@cygwin.com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner@cygwin.com
Mail-Followup-To: cygwin@cygwin.com
Delivered-To: mailing list cygwin@cygwin.com

--------------enig9B70BF089CC72F62D102BF09
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

On 11/29/2011 02:00 AM, Gary wrote:
> If I have a shell script which reads a file which does not have an end
> of line character at the end if it's only line, it does not read
> anything.

Not cygwin specific.

>=20
> For example:
> ,----
> | #!/bin/sh
> |=20
> | fileName=3D"test.xml"

Your example didn't tell us the contents of test.xml.  But this is
reproducible even without knowing the contents of test.xml.

> | retVal=3D"Z"
> |=20
> | exec 10<&0
> | exec < $fileName
> | while read configLine

read(1) is required to return non-zero status when it encounters a
partial line (one with no end of line character).  Which means that
since testXML had no newline character, read never returned status 0,
which means you never entered the body of the while loop,

> | do
> |     retVal=3D"A"

which means retVal was never assigned.

> | done
> | exec 0<&10 10<&-
> |=20
> | echo $retVal

So this is echoing an unassigned variable, as required by POSIX.

> |=20
> | exit 0
> `----
>=20
> If it's expected behaviour, what's the workaround?

Never feed 'read' unterminated input.  Always end your text files with a
newline.

--=20
Eric Blake   eblake@redhat.com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org


--------------enig9B70BF089CC72F62D102BF09
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)
Comment: Public key at http://people.redhat.com/eblake/eblake.gpg
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBCAAGBQJO1PeNAAoJEKeha0olJ0NqdV4H/RYpmsg46Zx/dLJ2lvs/u5hu
7fjB7ikXTuTIk856yhlOIgD8NRFGY2hD6sh4yv5wnwo3xTWdJeiHTCaF8Jt0YHzI
wL/aXiYfacrLv75IyQD5QydjSHodPblCE1De6GpXWcLkqz3nwG9Iz0PtaWfcSCn1
eAVAA9fY94D/gUx0iUZZsldA+GB0Jqy//dvWfbrglHXP5copwpxsv2o8pwwzRwXm
hBQYilYs2m+FqCBgnJU/cAu5PiQpa++sQ26L8FAzK/7NGJUGPbm5F9p5r8n2TcXh
1OK+l+RzxUzSyfdKJf6XGF8727beZim/lNeX7sx6KvYhYr4KiTET94wUa30O+Og=
=npzz
-----END PGP SIGNATURE-----

--------------enig9B70BF089CC72F62D102BF09--
