0

I have a script like below. My goal is write to a log file terminating with the current date and time as filename and spool to the file:

#!/bin/bash

year=`date +%Y`
month=`date +%m`
day=`date +%d`
prefixe=$month$day$year
logfile="/home/oracle/logs/exec_proc_daily_20_"$prefixe.log

sqlplus / "as sysdba" <<EOF
spool on
spool $logfile
execute Proc_RFC_MAJ_MV_ITIN;
execute Proc_RFC_MAJ_MV_REFGEO;
commit;
quit
EOF

When I execute the script, the spool $logfile gives an error. No log is created. But it works when I use something like spool exec_proc_daily_2o.log. Why is the variable $logfile not replace.

1
  • Replacing sqlplus / "as sysdba" with cat and running your script suggests that the variable replacement is working as expected... Commented Oct 24, 2016 at 15:55

3 Answers 3

3

[Edited with working example] Pass a variable to sqlplus:

demo.sh

export myvar1="hello"
export myvar2=123
sqlplus "/ as sysdba" @demo.sql $myvar1 $myvar2
echo "---shell is complete---"

demo.sql

select 'first variable is &1 and second is &2'
from dual;
exit
Sign up to request clarification or add additional context in comments.

2 Comments

Hi @Beege. Thanks for your input. I've tried your approach but it doesn't work. I get the message of incorrect usage of sqlplus command with suggestions on how to use it.
works in linux env, I have made two changes as I don't like to refer $1 and $2 in my SQL script. ``` define myvar1='&1' define myvar2='&2' select 'first variable is &&myvar1 and second is &&myvar2' from dual; exit ```
2

After doing alot of digging, I found the solution to the problem. On the line sqlplus / "as sysdba" <<EOF, I passed the variable like this sqlplus / as sysdba <<EOF >$logfile. So the complete code should be:

#!/bin/bash

year=`date +%Y`
month=`date +%m`
day=`date +%d`
prefixe=$month$day$year
logfile="/home/oracle/logs/exec_proc_daily_20_"$prefixe.log

sqlplus / "as sysdba" <<EOF >$logfile
spool on
spool $logfile
execute Proc_RFC_MAJ_MV_ITIN;
execute Proc_RFC_MAJ_MV_REFGEO;
commit;
quit
EOF

1 Comment

Your sqlplus command is redirecting the output of sqlplus to the $logfile.
1

@Beege answer works in Linux env. I have made few changes as I don't I like to refer to &1 and &2 in SQL script everywhere.

myvar1="hello"
myvar2=123
sqlplus username/password@SID @oracle_variabling_passing.sql $myvar1 $myvar2
echo "---shell is complete---"

oracle_variabling_passing.sql:

define first_var=&1
define second_var=&2
select 'first variable is &&first_var and second is &&second_var'
from dual;
exit

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.