public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* Shell script - is this expected behaviour?
@ 2011-11-29 10:04 Gary
  2011-11-29 15:43 ` Eric Blake
  0 siblings, 1 reply; 8+ messages in thread
From: Gary @ 2011-11-29 10:04 UTC (permalink / raw)
  To: cygwin

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.

For example:
,----
| #!/bin/sh
| 
| fileName="test.xml"
| retVal="Z"
| 
| exec 10<&0
| exec < $fileName
| while read configLine
| do
|     retVal="A"
| done
| exec 0<&10 10<&-
| 
| echo $retVal
| 
| exit 0
`----

If it's expected behaviour, what's the workaround?

-- 
Gary        Please do NOT send me 'courtesy' replies off-list.


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Shell script - is this expected behaviour?
  2011-11-29 10:04 Shell script - is this expected behaviour? Gary
@ 2011-11-29 15:43 ` Eric Blake
  2011-11-29 17:13   ` Gary
  2011-11-29 18:38   ` Nellis, Kenneth
  0 siblings, 2 replies; 8+ messages in thread
From: Eric Blake @ 2011-11-29 15:43 UTC (permalink / raw)
  To: cygwin

[-- Attachment #1: Type: text/plain, Size: 1264 bytes --]

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 #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 620 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Shell script - is this expected behaviour?
  2011-11-29 15:43 ` Eric Blake
@ 2011-11-29 17:13   ` Gary
  2011-11-29 18:38   ` Nellis, Kenneth
  1 sibling, 0 replies; 8+ messages in thread
From: Gary @ 2011-11-29 17:13 UTC (permalink / raw)
  To: cygwin

Eric Blake wrote:
> 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.

Okay, thanks. That's what I was wondering.

>> If it's expected behaviour, what's the workaround?
>
> Never feed 'read' unterminated input.  Always end your text files with a
> newline.

Yeah, sadly the input file isn't under my control. I ended up just
stuffing a newline on the end regardless. I would read the file first to
find out if it was necessary, but...

-- 
Gary        Please do NOT send me 'courtesy' replies off-list.


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: Shell script - is this expected behaviour?
  2011-11-29 15:43 ` Eric Blake
  2011-11-29 17:13   ` Gary
@ 2011-11-29 18:38   ` Nellis, Kenneth
  2011-11-29 20:23     ` Tim McDaniel
  1 sibling, 1 reply; 8+ messages in thread
From: Nellis, Kenneth @ 2011-11-29 18:38 UTC (permalink / raw)
  To: cygwin

From: Eric Blake [mailto:eblake@redhat.com] 
Subject: 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.

<snip>

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

When I can't control the contents of the input file, I pipe it through
a filter program I wrote that adds a final newline where the last
character of a stream is not a newline.
--Ken Nellis

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: Shell script - is this expected behaviour?
  2011-11-29 18:38   ` Nellis, Kenneth
@ 2011-11-29 20:23     ` Tim McDaniel
  2011-11-29 23:13       ` DSB
  2011-11-30 20:33       ` Nellis, Kenneth
  0 siblings, 2 replies; 8+ messages in thread
From: Tim McDaniel @ 2011-11-29 20:23 UTC (permalink / raw)
  Cc: cygwin

Nellis, Kenneth wrote:
> From: Eric Blake 
>> Never feed 'read' unterminated input.  Always end your text files
>> with a newline.
>
> When I can't control the contents of the input file, I pipe it
> through a filter program I wrote that adds a final newline where the
> last character of a stream is not a newline.

I don't have the time to experiment at the moment, but I'm pretty sure
that some of the standard tools append a line terminator if it's not
already on the last line of their input.  sed or awk or gawk, maybe?
Anyway, if you can stumble on such a program, it can save you having
to write and maintain and distribute a filter to all your
environments.

-- 
Tim McDaniel

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Shell script - is this expected behaviour?
  2011-11-29 20:23     ` Tim McDaniel
@ 2011-11-29 23:13       ` DSB
  2011-11-30 20:33       ` Nellis, Kenneth
  1 sibling, 0 replies; 8+ messages in thread
From: DSB @ 2011-11-29 23:13 UTC (permalink / raw)
  To: cygwin

On Tue, Nov 29, 2011 at 3:15 PM, Tim McDaniel <tmcd@panix.com> wrote:
> I don't have the time to experiment at the moment...

Likewise, but it seems likely some existing utilities like "line" or
"head -n1" might do the job. E.g.

foo=$(line < file)
foo=$(head -n1 file)

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 8+ messages in thread

* RE: Shell script - is this expected behaviour?
  2011-11-29 20:23     ` Tim McDaniel
  2011-11-29 23:13       ` DSB
@ 2011-11-30 20:33       ` Nellis, Kenneth
  1 sibling, 0 replies; 8+ messages in thread
From: Nellis, Kenneth @ 2011-11-30 20:33 UTC (permalink / raw)
  To: cygwin

From: Tim McDaniel
Subject: RE: Shell script - is this expected behaviour?

>I don't have the time to experiment at the moment, but I'm pretty sure
>that some of the standard tools append a line terminator if it's not
>already on the last line of their input.  sed or awk or gawk, maybe?
>Anyway, if you can stumble on such a program, it can save you having
>to write and maintain and distribute a filter to all your
>environments.

FWIW, "grep ^" seems to do the trick.
--Ken Nellis

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Shell script - is this expected behaviour?
@ 2011-11-29 16:51 Fergus
  0 siblings, 0 replies; 8+ messages in thread
From: Fergus @ 2011-11-29 16:51 UTC (permalink / raw)
  To: Cygwin ML; +Cc: Fergus

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

Yay.
Please could /etc/setup/timestamp be terminated?
Currently it is not, causing minor grief e.g. when using a shell script 
to check for updates.
Fergus


--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2011-11-30 16:55 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-11-29 10:04 Shell script - is this expected behaviour? Gary
2011-11-29 15:43 ` Eric Blake
2011-11-29 17:13   ` Gary
2011-11-29 18:38   ` Nellis, Kenneth
2011-11-29 20:23     ` Tim McDaniel
2011-11-29 23:13       ` DSB
2011-11-30 20:33       ` Nellis, Kenneth
2011-11-29 16:51 Fergus

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).