简体   繁体   中英

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.

Here is what the original SQLPLUS script was prompting for:

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 :

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

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 ' '. 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?

Easiest is to keep the interactions from the sql script. 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.

But yes, the answer on your question: YES, take the interactions out and prepare the decisions in the calling shell.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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