sqlplus中调用shell_(转)shell 调用sqlplus各种情况示例
一、最簡單的shell里調(diào)用sqlplus.
$ vi test1.sh
#!/bin/bash
sqlplus -S /nolog > result.log < set heading off feedback off pagesize 0 verify off echo off
conn u_test/iamwangnc
select * from tab;
exit
EOF
$ chmod +x test1.sh
$ ./test1.sh
二、把sqlplus執(zhí)行結(jié)果傳遞給shell方法一
注意sqlplus段使用老板鍵`了, 賦變量的等號兩側(cè)不能有空格.
$ vi test2.sh
#!/bin/bash
VALUE=`sqlplus -S /nolog < set heading off feedback off pagesize 0 verify off echo off numwidth 4
conn u_test/iamwangnc
select count(*) from tab;
exit
EOF`
if [ "$VALUE" -gt 0 ]; then
echo "The number of rows is $VALUE."
exit 0
else
echo "There is no row in the table."
fi
$ chmod +x test2.sh
$ ./test2.sh
三、把sqlplus執(zhí)行結(jié)果傳遞給shell方法二
注意sqlplus段使用 col .. new_value .. 定義了變量并帶參數(shù)exit, 然后自動賦給了shell的$?
$ vi test3.sh
#!/bin/bash
sqlplus -S /nolog > result.log < set heading off feedback off pagesize 0 verify off echo off numwidth 4
conn u_test/iamwangnc
col coun new_value v_coun
select count(*) coun from tab;
exit v_coun
EOF
VALUE="$?"
echo "The number of rows is $VALUE."
$ chmod +x test3.sh
$ ./test3.sh
四、把shell程序參數(shù)傳遞給sqlplus
$1表示第一個參數(shù), sqlplus里可以直接使用, 賦變量的等號兩側(cè)不能有空格不能有空格.
$ vi test4.sh
#!/bin/bash
NAME="$1"
sqlplus -S u_test/iamwangnc < select * from tab where tname = upper('$NAME');
exit
EOF
$ chmod +x test4.sh
$ ./test4.sh ttt
五、為了安全要求每次執(zhí)行shell都手工輸入密碼
$ vi test5.sh
#!/bin/bash
echo -n "Enter password for u_test:"
read PASSWD
sqlplus -S /nolog < conn u_test/$PASSWD
select * from tab;
exit
EOF
$ chmod +x test5.sh
$ ./test5.sh
六、為了安全從文件讀取密碼
對密碼文件設(shè)置權(quán)限, 只有用戶自己才能讀寫.
$ echo 'iamwangnc' > u_test.txt
$ chmod g-rwx,o-rwx u_test.txt
$ vi test6.sh
#!/bin/bash
PASSWD=`cat u_test.txt`
sqlplus -S /nolog < conn u_test/$PASSWD
select * from tab;
exit
EOF
$ chmod +x test6.sh
$ ./test6.sh
--End--
自己試驗的內(nèi)容:
#!/bin/sh
PATH=$PATH:$HOME/bin
export PATH
export ORACLE_SID=oravm
export ORACLE_BASE=/opt/oracle
export ORACLE_HOME=/opt/oracle/9.2.0
export LD_LIBRARY_PATH=/opt/oracle/9.2.0/lib:/lib:/usr/lib:/usr/local/lib:/usr/X11R6/lib
export TNS_ADMIN=/opt/oracle/9.2.0/network/admin
export ORA_NLS33=/opt/oracle/9.2.0/ocommon/nls/admin/data
export ORACLE_OWNER=oracle
export ORACLE_TERM=xterm
export PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/opt/oracle/9.2.0/bin
export LANG=zh_CN.gb2312
export NLS_LANG="Simplified Chinese_china".ZHS16GBK
export NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'
/*將/home/oracle/archlog/下的全部文件(實際上是歸檔日志文件)讀入系統(tǒng),結(jié)合數(shù)據(jù)字典,然后寫入system.logmnr表中)*/
for fname in /home/oracle/archlog/*; do
echo "Processing $fname!"
echo "connect /as sysdba" > /home/oracle/temp.sql
echo? "EXECUTE dbms_logmnr.add_logfile('$fname',dbms_logmnr.addfile);" >> /home/oracle/temp.sql
echo "EXECUTE dbms_logmnr.start_logmnr ( DictFileName=>'/opt/oracle/9.2.0/Oraclelogs/v816dict.ora')" >> /home/oracle/temp.sql
echo? "insert into system.logmnr("SCN","CSCN","TIMESTAMP","COMMIT_TIMESTAMP","THREAD#","LOG_ID","XIDUSN","XIDSLT","XIDSQN","PXIDUSN","PXIDSLT","PXIDSQN","RBASQN","RBABLK","RBABYTE","UBAFIL","UBABLK","UBAREC","UBASQN","ABS_FILE#","REL_FILE#","DATA_BLK#","DATA_OBJ#","DATA_OBJD#","SEG_OWNER","SEG_NAME","SEG_TYPE","SEG_TYPE_NAME","TABLE_SPACE","ROW_ID","SESSION#","SERIAL#","USERNAME","SESSION_INFO","TX_NAME","ROLLBACK","OPERATION","OPERATION_CODE","SQL_REDO","SQL_UNDO","RS_ID","SEQUENCE#","SSN","CSF","INFO","STATUS","REDO_VALUE","UNDO_VALUE","SQL_COLUMN_TYPE","SQL_COLUMN_NAME","REDO_LENGTH","REDO_OFFSET","UNDO_LENGTH","UNDO_OFFSET")(select * from v\$logmnr_contents);" >> /home/oracle/temp.sql
echo "exit;" >> /home/oracle/temp.sql
/opt/oracle/9.2.0/bin/sqlplus /nolog @/home/oracle/temp.sql
echo The SQL file $fname finished!
echo ---------------------------------------------------------------
done
文件:
get_arch.rar
大小:
1KB
下載:
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的sqlplus中调用shell_(转)shell 调用sqlplus各种情况示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python删除文件与目录的方法
- 下一篇: 天线3db波束宽度_天线波束宽度计算公式