This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Shell script - is this expected behaviour?


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

Attachment: signature.asc
Description: OpenPGP digital signature


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]