简体   繁体   English

如何将shell脚本的输入传递给sqlplus?

[英]How to pass input from shell script to sqlplus?

I have been asked to change a part of the existing SQLPLUS script to a shell script to allow for more options. 我被要求将现有SQLPLUS脚本的一部分更改为shell脚本以允许更多选项。

Here is what the original SQLPLUS script was prompting for: 以下是原始SQLPLUS脚本提示的内容:

SPOOL results.txt;
WHENEVER OSERROR EXIT 9;
WHENEVER SQLERROR EXIT SQL.SQLCODE;
PROMPT
PROMPT This script upgrades the database to the most recent version of eV8.0.
SET VERIFY OFF;
SET SERVEROUTPUT ON;

ACCEPT continue_flag CHAR PROMPT 'Are You ready to continue Y/N? '
PROMPT

My shell script now looks like : 我的shell脚本现在看起来像:

 #!/bin/sh

echo "Please enter database username"
read eval_user
echo "Please enter database password"
read eval_pass
echo "Please enter the database name"
read db_name

$ORACLE_HOME/bin/sqlplus -s /nolog <<-EOF >> ${LOGFILE}
connect $eval_user/$eval_pass@$db_name

set serveroutput on format wrapped
set pages 0 lines 300 trimout on trimspool on
set echo off feedback off sqlprompt ''

SPOOL results.txt;
WHENEVER OSERROR EXIT 9;
WHENEVER SQLERROR EXIT SQL.SQLCODE;
PROMPT
SET VERIFY OFF;
SET SERVEROUTPUT ON;

ACCEPT continue_flag CHAR PROMPT 'Are You ready to continue Y/N? '
PROMPT

I get this error when the SQLPLUS prompt line is reached: 到达SQLPLUS提示行时出现此错误:

Are You ready to continue Y/N? You have entered an invalid response. Exiting.
BEGIN
*
ERROR at line 1:
ORA-06501: PL/SQL: program error
ORA-06512: at line 13

I am new to sqlplus, so I know it is something to do with the settings I set in the beginning, if I set echo off feedback off sqlprompt ' '. 我是sqlplus的新手,所以我知道这与我在开头设置的设置有关,如果我设置echo关闭sqlprompt''的反馈。 But, on removing the line, the script just hangs. 但是,在删除该行时,脚本就会挂起。

Should I take the prompts completely out of SQLPLUS and use it only in the shell part? 我应该完全从SQLPLUS中提取提示并仅在shell部分中使用它吗?

Easiest is to keep the interactions from the sql script. 最简单的方法是保持与sql脚本的交互。 It does want to read something that you currently do not give it. 它确实想要阅读你目前没有给出的东西。 The here document is a fine way to pass variables. 这里的文档是传递变量的好方法。 Don't forget the effect that it has on shell special characters ... As always, there are more ways to do the same. 不要忘记它对shell特殊字符的影响......一如既往,还有更多方法可以做到这一点。

But yes, the answer on your question: YES, take the interactions out and prepare the decisions in the calling shell. 但是,是的,您的问题的答案是:是的,采取交互,并在调用shell中准备决策。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM