问题
I have a shell script, when I execute it, I get the error
syntax error at line 34 : `<<' unmatched in ksh script
column_name=`sqlplus -s $BASE_DB_CONN<<!!
WHENEVER SQLERROR exit ROLLBACK
set SQLPROMPT ''
set heading off
set pagesize 1000
set linesize 5000
set feedback off
set define on
set verify off
@smm9_stream_map.sql $STREAM_NAME $FIELD_NAME"
exit
!!`;
To my surprise the same thing works in the while loop altered script as below
while [ $a -lt ${NO_LOOP} ]
do
column_name=`sqlplus -s $BASE_DB_CONN<<!!
WHENEVER SQLERROR exit ROLLBACK
set SQLPROMPT ''
set heading off
set pagesize 1000
set linesize 5000
set feedback off
set define on
set verify off
@smm9_stream_map.sql $STREAM_NAME $FIELD_NAME"
exit
!!`;
a=`expr $a + 1`
done
Why does the second script work but not the first one?
回答1:
The heredoc terminator must be the only text on that line, no other whitespace allowed (*)
column_name=$(sqlplus -s $BASE_DB_CONN<<!!
....
exit
!!
)
(*) the exception being, for heredocs using <<-
, any leading tabs will be stripped from the heredoc, including from the terminator. I see in my ksh93 man page:
If # is appended to <<, then leading spaces and tabs will be stripped off the first line of the document and up to an equivalent indentation will be stripped from the remaining lines and from word.
来源:https://stackoverflow.com/questions/20186743/getting-syntax-error-at-line-34-unmatched-in-ksh-script