我不知道你的代码有什么问题。它应该工作。我测试了它:
$ sqlplus '/as sysdba' <<_EOF1_ >sql.log
spool '/home/oracle/test1.log'
select * from dual;
spool off;
exit;
_EOF1_
输出
$ cat sql.log
SQL*Plus: Release 11.2.0.4.0 Production on Mon Nov 6 15:18:42 2017
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, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
SQL> SQL>
D
-
X
SQL> SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
输出 2
$ cat /home/oracle/test1.log
SQL> select * from dual;
D
-
X
SQL> spool off;
有两种选择可以实现您的目标。您将两者合二为一。有什么理由吗?
选项1
$ sqlplus / as sysdba << EOF > /dev/null
spool test.out
select * from dual;
spool off
EOF
输出
$ cat test.out
SQL> select * from dual;
D
-
X
SQL> spool off
选项 2
$ sqlplus / as sysdba << EOF > test.out
> select * from dual;
> EOF
输出
$ cat test.out
SQL*Plus: Release 11.2.0.4.0 Production on Mon Nov 6 15:12:05 2017
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, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options
SQL>
D
-
X
SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management, OLAP,
Data Mining and Real Application Testing options