目标:从shell提示符下以sql脚本文件路径作为参数调用sqlplus
方案:
以root用户身份登录,并希望以oracle身份执行sql脚本文件
命令:
sudo -u oracle bash -c ". ~/.bash_profile; sqlplus / as sysdba@/tmp/downloads/oracle/instl/script/createschema.sql
预期:createschema.sql中的 sql命令将被执行
实际:仅获得sql提示
此外,尝试:
一个)
sudo -u oracle bash -c ". ~/.bash_profile; sqlplus@/tmp/downloads/oracle/instl/script/createschema.sql / as sysdba
b)
sudo -u oracle bash -c ". ~/.bash_profile; sqlplus -s /as sysdba << EOF
每当sqlerror退出sql.sqlcode;关闭回声;出发/tmp/downloads/oracle/inSTL/script/createschema.sql;导出; EOF”
但是在a)和b)中都出现错误。
请指导我如何使用sql脚本文件路径作为参数从shell提示符中调用sqlplus。
最佳答案
例子1
#!/bin/sh
username=\"Scott\"
password=\"@T!ger\"
host=10.x.xx.xxx
port=1521
service=esmd
ezconnect=$host:$port/$service
echo username: $username
echo password: $password
echo host: $host
echo port: $port
echo service: $servive
echo ezconnect $ezconnect
echo -e 'show user \n select 1 from dual; \n select sysdate from dual; \nexit;' | sqlplus -s $username/$password@$ezconnect
输出:
oracle@esmd:~> ./test_echo.sh
username: "Scott"
password: "@T!ger"
host: 10.x.xx.xxx
port: 1521
service:
ezconnect 10.x.xx.xxx:1521/esmd
USER is "Scott"
1
----------
1
SYSDATE
---------
19-FEB-20
oracle@esmd:~>
例子2
#!/bin/sh
username=\"Scott\"
password=\"@T!ger\"
echo username: $username
echo password: $password
testoutput=$(sqlplus -s $username/$password << EOF
set pagesize 0 feedback off verify off heading off echo off;
show user
SELECT to_char(sysdate,'DD-MM-YYYY HH24:MI')||' Test passed' from dual;
@ulcase1.sql
exit;
EOF
)
echo $testoutput
输出:
oracle@esmd:~> ./test_Upper_case.sh
username: "Scott"
password: "@T!ger"
USER is "Scott" 19-02-2020 15:08 Test passed
oracle@esmd:~>
示例3 test.sh
#!/bin/bash
sudo -H -E -u oracle -s "/opt/oracle/test_Upper_case.sh"
输出量
esmd:~ # ./test.sh
username: "Scott"
password: "@T!ger"
USER is "Scott" 19-02-2020 15:50 Test passed
/opt/oracle/test_Upper_case.sh
!/bin/sh
username=\"Scott\"
password=\"@T!ger\"
echo username: $username
echo password: $password
testoutput=$($ORACLE_HOME/bin/sqlplus -s $username/$password << EOF
set pagesize 0 feedback off verify off heading off echo off;
show user
SELECT to_char(sysdate,'DD-MM-YYYY HH24:MI')||' Test passed' from dual;
@/opt/oracle/ulcase1.sql
exit;
EOF
)
echo $testoutput