delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2011/11/29/10:18:15

X-Recipient: archive-cygwin AT delorie DOT 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 AT redhat DOT com>
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:8.0) Gecko/20111115 Thunderbird/8.0
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: Re: Shell script - is this expected behaviour?
References: <jb26vu$5tc$1 AT dough DOT gmane DOT org>
In-Reply-To: <jb26vu$5tc$1@dough.gmane.org>
OpenPGP: url=http://people.redhat.com/eblake/eblake.gpg
X-IsSubscribed: yes
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Id: <cygwin.cygwin.com>
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT 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 AT redhat DOT 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--

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019