tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[PATCH] Fix /bin/sh behaviour with 'here document'



Simple script
  cat << xx
  Test
  xx
w/o <newline> at the end of file produces incorrect output like
  Test
  xx
in /bin/sh. /usr/pkg/bin/bash works normally. Although the SuSv3 in 2.7.4
somehow requires a <newline>, the actual file or stream could be ended by EOF
w/o <newline>. Probably EOF has higher priority, but I don't know. Actually the
only two shells what I saw have described behaviour: from NetBSD and from old
Ubuntu. New ones consider <newline> at the end of file like recommended.
---
 bin/sh/parser.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/bin/sh/parser.c b/bin/sh/parser.c
index 0017942..80ae6de 100644
--- a/bin/sh/parser.c
+++ b/bin/sh/parser.c
@@ -1170,7 +1170,7 @@ checkend: {
 
                                p = line;
                                for (q = eofmark + 1 ; *q && *p == *q ; p++, 
q++);
-                               if (*p == '\n' && *q == '\0') {
+                               if ((*p == '\0' || *p == '\n') && *q == '\0') {
                                        c = PEOF;
                                        plinno++;
                                        needprompt = doprompt;
-- 
1.5.2.5



Home | Main Index | Thread Index | Old Index