@script.sql
的情况下呼应命令输入到sqlplus中吗?电流脚本:
$ cat test.sh
#!/bin/bash
LOG=/home/oracle/output.log
sqlplus hr/hr <<EOF > $LOG
set echo on
select 1 from dual;
QUIT
EOF`
电流输出:
$ cat output.log
SQL*Plus: Release 11.2.0.4.0 Production on Tue Mar 1 15:01:12 2016
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining,
Oracle Database Vault and Real Application Testing options
SQL> SQL>
1
----------
1
我想要什么:
$ cat output.log
SQL*Plus: Release 11.2.0.4.0 Production on Tue Mar 1 15:02:02 2016
Copyright (c) 1982, 2013, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Oracle Label Security, OLAP, Data Mining,
Oracle Database Vault and Real Application Testing options
SQL> SQL> SQL> select 1 from dual;
1
----------
1
当SQL*Plus是从您的TTY读取命令时,输入的回声实际上是由您的TTY而不是SQL*Plus处理的。 如果SQL*Plus处理了回声,则每当您手动键入命令时,您都会看到命令两次(一旦您键入它,一次,一次回声时)。在进行ADDICH,该选项仅在运行脚本文件时适用,而不是从stdin读取。
TERMOUT
是一个脚本:
/dev/stdin
Try
sqlplus scott/tiger @/dev/stdin <<EOF
SET TERMOUT ON ECHO ON
SELECT SYSDATE FROM dual;
EOF
而不是重定向您的Stdout:
spool
扩展 @laster @先生的答案。 他的构造在Solaris上非常完美。
如何在Linux上(Oracle Linux服务器版本7.9),这会导致每个命令在SQLPlus中运行两次,即使它仅回声一次。请参阅下面:
#!/bin/bash
LOG=/home/oracle/output.log
sqlplus hr/hr <<EOF
set echo on term on
spool $LOG
select 1 from dual;
QUIT
EOF
notice如何有两条Systimestamp线,具有不同的时间戳,这意味着选择被执行两次。当在Solaris上运行相同的块时,这双重执行不会发生。 linux解决方法是将构造修改为:
$ sqlplus -l / as sysdba @/dev/stdin <<-EOF
> set echo on termout on
> select systimestamp from dual;
> EOF
SQL*Plus: Release 19.0.0.0.0 - Production on Wed Feb 23 14:33:03 2022
Version 19.11.0.0.0
Copyright (c) 1982, 2020, Oracle. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.11.0.0.0
SQL> select systimestamp from dual;
SYSTIMESTAMP
---------------------------------------------------------------------------
23-FEB-22 02.33.03.975542 PM -08:00
SQL> SQL>
SYSTIMESTAMP
---------------------------------------------------------------------------
23-FEB-22 02.33.03.976106 PM -08:00
SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.11.0.0.0
如果您使用的是powershell,则可以使用带有您想要运行的查询的管道
$ cat /dev/stdin <<-EOF | sqlplus -l / as sysdba @/dev/stdin
> set echo on termout on
> select systimestamp from dual;
> EOF
SQL*Plus: Release 19.0.0.0.0 - Production on Wed Feb 23 14:38:08 2022
Version 19.11.0.0.0
Copyright (c) 1982, 2020, Oracle. All rights reserved.
Connected to:
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.11.0.0.0
SQL> select systimestamp from dual;
SYSTIMESTAMP
---------------------------------------------------------------------------
23-FEB-22 02.38.08.931790 PM -08:00
SQL> Disconnected from Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production
Version 19.11.0.0.0