Shell script - is this expected behaviour?

Eric Blake eblake@redhat.com
Tue Nov 29 15:43:00 GMT 2011


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.

> 
> For example:
> ,----
> | #!/bin/sh
> | 
> | fileName="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="Z"
> | 
> | 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="A"

which means retVal was never assigned.

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

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

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

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

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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 620 bytes
Desc: OpenPGP digital signature
URL: <http://cygwin.com/pipermail/cygwin/attachments/20111129/db5a61ac/attachment.sig>


More information about the Cygwin mailing list