DBA經常會部署一些sh腳本登陸Oracle數據庫查詢v$動態視圖得到一些東西來實際管理自動化的目的,但在sh腳本中寫ORACLE SQL語句時,如果語句查詢v$視圖,直接寫v$XXXX是不能成功的,shell會將$當成一個參數來處理。
以下面一段簡單的sh腳本為例:/home/oracle/s_parameter.sh
sqlplus /nolog<
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 fromgvorder 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<
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就可以得到正確的結果(注意上面表格中紅色的“\”號)