转载至http://blog.csdn.net/ljunjie82/article/details/9881937
DBA经常会部署一些sh脚本登陆Oracle数据库查询v$动态视图得到一些东西来实际管理自动化的目的,但在sh脚本中写ORACLE SQL语句时,如果语句查询v$视图,直接写v$XXXX是不能成功的,shell会将$当成一个参数来处理。
以下面一段简单的sh脚本为例:/home/oracle/s_parameter.sh
sqlplus /nolog<<EOF conn / as sysdba col inst_id for 99 col name for a48 col value for a64 set pagesize 1000 line 180 spool /home/oracle/1111.txt select inst_id,name,value from gv$system_parameter order by 1,2; spool off quit EOF |
当执行 sh /home/oracle/s_parameter.sh时,会报如下表或视图不存在的错误错:
SQL*Plus: Release 11.2.0.3.0 Production on Sun Aug 18 11:28:17 2013
Copyright (c) 1982, 2011, Oracle. All rights reserved.
SQL> Connected. SQL> SQL> SQL> SQL> SQL> SQL> select inst_id,name,value from gv order by 1,2 * ERROR at line 1: ORA-00942: table or view does not exist
SQL> SQL> Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options |
本来sh脚本文件中写的是gv$system_parameter的,但是执行时,只能识别到gv,$以后面的视图名都未被识别。
处理这个问题其实很简单,只需要在$前加上一个“\”转义符即可,例如
sqlplus /nolog<<EOF conn / as sysdba col inst_id for 99 col name for a48 col value for a64 set pagesize 1000 line 180 spool /home/oracle/1111.txt select inst_id,name,value from gv\$system_parameter order by 1,2; spool off quit EOF |
再执行/home/oracle/s_parameter.sh就可以得到正确的结果(注意上面表格中红色的“\”号)