Oracle 如何在bash脚本中将变量传递给sqlplus

Oracle 如何在bash脚本中将变量传递给sqlplus,oracle,bash,sqlplus,Oracle,Bash,Sqlplus,我有一个脚本如下。我的目标是写入日志文件,以当前日期和时间作为文件名终止,并将假脱机写入文件: #!/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 $logfil

我有一个脚本如下。我的目标是写入日志文件,以当前日期和时间作为文件名终止,并将假脱机写入文件:

#!/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

执行脚本时,spool$logfile会给出一个错误。未创建任何日志。但当我使用类似spool exec_proc_daily_2o.log的东西时,它就可以工作了。为什么$logfile变量没有被替换。

在做了大量挖掘之后,我找到了问题的解决方案。在sqlplus/as-sysdba行中,经过大量挖掘,我找到了问题的解决方案。在sqlplus/as sysdba[使用工作示例编辑]行上,将变量传递给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

[使用工作示例编辑]将变量传递给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

@Beege answer在Linux环境中工作。我做了一些更改,因为我不喜欢在SQL脚本中到处引用&1和&2

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

@Beege answer在Linux环境中工作。我做了一些更改,因为我不喜欢在SQL脚本中到处引用&1和&2

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

将sqlplus/as sysdba替换为cat并运行脚本表明变量替换工作正常…将sqlplus/as sysdba替换为cat并运行脚本表明变量替换工作正常…sqlplus命令将sqlplus的输出重定向到$logfile。您的sqlplus命令正在将sqlplus的输出重定向到$logfile.Hi@Beege。谢谢你的意见。我试过你的方法,但不管用。在linux环境中,我做了两个更改,因为我不喜欢在我的SQL脚本中引用$1和$2define myvar1='&1'define myvar2='&2'从dual中选择“第一个变量是&&myvar1,第二个是&&myvar2”;出口``Hi@Beege。谢谢你的意见。我试过你的方法,但不管用。在linux环境中,我做了两个更改,因为我不喜欢在我的SQL脚本中引用$1和$2define myvar1='&1'define myvar2='&2'从dual中选择“第一个变量是&&myvar1,第二个是&&myvar2”;出口```