LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Problem w/.bash script... need help (https://www.linuxquestions.org/questions/linux-newbie-8/problem-w-bash-script-need-help-4175538662/)

ampapa 04-03-2015 10:19 AM

Problem w/.bash script... need help
 
I'm trying to loop through a query in SQLDeveloper by passing the query a different Period and I'm having a problem getting the syntax correct.

Individually the following works correctly on its own:

Code:

#!/bin/bash
 for i in `seq -f "%02g" 1 5`;
    do
    echo $i
 done

$ ./test.sh
1
2
3
4
5


the following also executes correctly:

Quote:

sqlplus table1/$userpass@Mytable <<EOF
@./sqlcode.sql $i
EOF
but when I try to wrap the loop around my sqlplus command

Code:

#! /bin/sh
userpass=read
for i in `seq -f "%01g" 1 3`;
  do
        sqlplus -S  table1/$userpass@Mytable <<EOF
        @./sqlcode.sql $i
        EOF
done

I get the following error:

./test.sh: line 13: syntax error: unexpected end of file

Does anyone have any ideas on what's going on?

grail 04-03-2015 11:17 AM

Not sue I get the idea of the for loop? If you are simply using 1 - 3 the 01g does not appear to do anything for you.

userpass being set to the string read, was this your intention or were you hoping to ask the user for the password with the read command?

As for the sqlplus line, not sure I see the point of a here document for a single line. Also, the ./ is not required if the file is in the current directory.

Lastly, there are not 13 lines of code in your shell script so the error message would imply you are not showing the entire script.

For debugging the issue you could try placing set -xv as second line in script and see where your error is coming from.

ampapa 04-03-2015 12:22 PM

Thanks for the follow up.

The loop is for Periods 1-12, I simply want to run the same script 12 times for different periods of the month. The periods were originally formatted in 01,02, etc.

The password 'userpass' was just an easier place to enter the credentials from our TEST and PROD environments, I could have placed on the sqlplus line as easy.

Since the sql generates a large file, 10gig I didn't want to look at the spooling of data on the terminal and the only way to stop the output was passing in the file sqlcode.sql

Code:

!/bin/sh
set -xv
userpass=read
export ORACLE_HOME=/u01/app/oracle/product/11.2.0
export PATH=$PATH:$ORACLE_HOME/bin


for i in `seq 1 12`;
  do
    sqlplus table1/$userpass@Mytable <<EOF
    @sqlcode.sql $i
    EOF
 done

same error... sorry I originally pulled out the path info in the script... there are 13 lines of code.

/test.sh: line 14: syntax error: unexpected end of file

ampapa 04-03-2015 12:29 PM

I found the error, it was related to extra garbage on the script, must have come from copying from notepad++...

Is there an efficient way for copying to a bash script from notepad++ to avoid the problem I was having?

ampapa,

veerain 04-03-2015 01:05 PM

Quote:

Is there an efficient way for copying to a bash script from notepad++ to avoid the problem I was having?
You should try dos2unix command.

ampapa 04-03-2015 01:53 PM

Thanks, will do next time.

ampapa,

grail 04-04-2015 02:58 AM

notepad++ has the option to save as a nix file so you should be able to save it without problem.

i would also note that your here document won't work either as the ending is not at the start of the line and you did not preceed the start with a hyphen.

pan64 04-04-2015 03:56 AM

If I remember well the second, closing EOF (inside the for loop) must begin at the beginning of the line (there should be no space before)


All times are GMT -5. The time now is 10:07 AM.