Trying to follow the post from Shawn Fox describing the use of HERE document to pass variable from a shell script to nzsql.
Have the following code that is resulting in an error when executed from the command line.
#!/bin/sh
#
# -----------------------------------------------------------------------------------------------
# Only run this on the active host; otherwise exit
# -----------------------------------------------------------------------------------------------
#
dt=$(date +%Y%m%d --date='1 days ago')
#
while [ ! -e /snap/BIC/initial_load/today/$dt ]; do
# Sleep until directory exists/is created
sleep 1
done
#
#
extractdate=$(date +%Y-%m-%d --date='1 days ago')
executedate=`date +%Y-%m-%d`
echo "***********************************"
echo "** Insert to Load_Complete Table **"
echo "***********************************"
echo "*********************************************************************************************************************************************************************"
nzsql -d $NZ_DATABASE -vON_ERROR_STOP=1 <<Complete
insert into LOAD_COMPLETE_TEST (completion_dte,extract_dte) VALUES ('${executedate}','${extractdate}');
Complete
echo "********************************************"
echo "** Insert to Load_Complete Table Complete **"
echo "********************************************"
echo "*********************************************************************************************************************************************************************"
exit ${?}
the error returned is
error ^ found "COMPLETE" (at char 9) expecting a keyword
The insert is successfull, but I'd like to eliminate the error and don't seem to be able to do so. Any help is greatly appreciated.
The reason you get this error is that your 'Complete' to end the HERE document is not at the start of the line and you did not use <<-. The extra - at the end of <<- tells the shell to discard all TAB characters on the front of each line, so using <<- allows you to indent the script instead of forcing you to put the document aligned to the left of the screen.
So, you can either do it like this where the Complete starts at the beginning of the line:
nzsql -d $NZ_DATABASE -vON_ERROR_STOP=1 <<Complete
insert into LOAD_COMPLETE_TEST (completion_dte,extract_dte) VALUES ('${executedate}','${extractdate}');
Complete
or if you use <<-Complete instead of <<Complete you can add TAB characters to make your code more readable:
nzsql -d $NZ_DATABASE -vON_ERROR_STOP=1 <<-Complete
insert into LOAD_COMPLETE_TEST (completion_dte,extract_dte) VALUES ('${executedate}','${extractdate}');
Complete
I would use BASH for consistency, but that's a preference
I would use a "!" rather than "complete" - just a preference
make sure there are no extra spaces after the word "Complete" at the end - this causes a shell parsing error
I agree with David on using "#!/bin/bash" instead of "#!/bin/sh" .. on most systems /bin/sh is bash, but every now and then /bin/sh is actually the original Bourne shell and your script isn't going to work so well ![]()
I hate the ! convention for HERE documents though, it looks very unnatural to me. I generally use END
nzsql <<-END
print "Hello world!"
END
vs.
nzsql <<!
print "Hello world!"
!
I find the single ! harder to see as compared to END. Either way, ask 3 shell programmers for their opinions and you'll end up with 5 different opinions most likely. I also prefer ksh over bash but I'd recommend others to use bash due to it being available on every Unix system. Linux runs pdksh instead of the real ksh. pdksh is buggy and doesn't completely implement all the features of ksh, so between the two, bash is an easy choice over pdksh.
The recommendation to change to the bash shell made the biggest difference. David's caution to ensure that there were no spaces after the end of the termination string was also insightful as I had that problem too and would not have thought to look for it without Davi's comment. All of the Here document termination strings (END, Complete,and ! ) work with that change.
Thanks David and Shawn, your replies were timely, extremely useful, and very much appreciated.
