[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Bash error message for unterminated heredoc is unhelpful.
From: |
Richard Neill |
Subject: |
Bash error message for unterminated heredoc is unhelpful. |
Date: |
Sat, 28 Jun 2008 20:49:58 +0100 |
User-agent: |
Thunderbird 2.0.0.12 (X11/20080306) |
Dear All,
In some cases, bash gives exceptionally unhelpful error messages, of the
sort "Unexpected end of file". This is next-to-useless as a debugging
aid, since there is no way to find out where the error really lies.
I'm using bash version: bash-3.2-7mdv2008.1
Here are 2 shell scripts with examples.
bug-example.sh demonstrates the problem.
bug-example2.sh is where bash gets it right.
Thanks very much,
Richard
[rjn@pistachio ~]$ cat bug-example.sh
-------------------------------------------------------------
#!/bin/bash
#This is an example of bash being unhelpful with syntax errors.
function usage () {
cat <<-EOT
This help text is a heredoc
EOT
#NOTE that the line above contains a trailing TAB, and
# is therefore NOT valid as the end of the heredoc.
}
usage
echo "A long script goes here..."
echo "Many lines later..."
exit 0
#This script generates the following error:
# ./bug-example.sh: line 37: syntax error: unexpected end of file
#That is really not helpful, since there's no way to track down where
#the error started. Line 37 is not interesting. What we need is a
#warning about line 6.
#At a minimum, we should get this error message:
# ./bug-example.sh: line 37: syntax error: unexpected end of file
# (started at line 6)
#Better, would be this error message:
# ./bug-example.sh: line 6: syntax error: unterminated heredoc.
#An additional buglet is that in fact, trailing whitespace after
#a heredoc terminator should probably be ignored anyway?
-------------------------------------------------------------
[rjn@pistachio ~]$ cat bug-example2.sh
-------------------------------------------------------------
#!/bin/bash
#This is an example of bash being *helpful* with syntax errors.
X=$((1 + 2) #NOTE missing trailing ).
echo "X is $X" #Should print 'X is 3'
echo "A long script goes here..."
echo "Many lines later..."
exit 0
#This script gets it right with the error message.
# ./bug-example2.sh: line 5: unexpected EOF while looking for
# matching `)'
# ./bug-example2.sh: line 20: syntax error: unexpected end of file
#So, we can quickly find the bug.
-------------------------------------------------------------
- Bash error message for unterminated heredoc is unhelpful.,
Richard Neill <=