Skip navigation
17354 Views 4 Replies Latest reply: Dec 19, 2009 2:13 PM by dprittie RSS
dprittie New Enzee 38 posts since
Oct 2, 2009
Currently Being Moderated

Dec 19, 2009 8:28 AM

Passing parameters from shell script to nzsql error using here document

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.

  • Shawn Fox Enzee Exraordinaire 1,333 posts since
    Aug 15, 2006

    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

  • David Birmingham Active Enzee 426 posts since
    Sep 24, 2007

    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

  • Shawn Fox Enzee Exraordinaire 1,333 posts since
    Aug 15, 2006

    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.

More Like This

  • Retrieving data ...

Bookmarked By (0)