收割机地头教学:shell中操作sqlplus

来源:百度文库 编辑:偶看新闻 时间:2024/04/29 16:13:30
shell中操作sqlplus2011-01-05 21:56一、最简单的shell里调用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执行结果传递给shell方法一注意sqlplus段使用老板键`了, 赋变量的等号两侧不能有空格.$ 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执行结果传递给shell方法二注意sqlplus段使用 col .. new_value .. 定义了变量并带参数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程序参数传递给sqlplus$1表示第一个参数, sqlplus里可以直接使用, 赋变量的等号两侧不能有空格不能有空格.$ 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五、为了安全要求每次执行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六、为了安全从文件读取密码对密码文件设置权限, 只有用户自己才能读写.$ 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--  自己试验的内容:#!/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/下的全部文件(实际上是归档日志文件)读入系统,结合数据字典,然后写入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

ps:解析Shell中,块的开始和结束
su - oracle -c "sqlplus username/password"<其中:
su: 切换用户
-: 使用orcale的环境设置
-c: 执行后面的命令
<:因为sqlplus要输入用户名和密码所以接下去的两行分别输入。
当第三行出现EOF的时候,shell认为输入结束;并将输入传递给sqlplus命令
eof可以是任何字符串 比如"laldf"那么当你输入单独一行laldf时"shell认为输入结束,但是必须表示块开始必须使用<<
开始和结束要匹配这个符号“<<”后面的内容
举例子:
代码:
su - oracle -c "sqlplus username/password" <abc

//这个abc是shell用来识别的开始
此时屏幕显示:sqlplus username/password
然后你输入:user001
                     user001
遇见代码:abc                     ,表示结束            

今天在改一个脚本的时候突然遇到一个问题,SQLPLUS在登录的时候出现"<< is not matched "的错误

细细看来,原来是可以,经过改变以后只是位置不一样,改之前是在for循环的外面登录,改之后在
for循环内登录,但是我想也不应该影响才对的

改之前的登录执行方式:
sqlplus -s "username/password@alias" <TRUNCATE TABLE ZHJS_TP_UNIQUE_ID;
exit
EOF

改了好几个方式还是不行,到最后实在受不了,用google搜索得了,烦的,晕死了,不搜还好,一搜
结果就出来了,呵呵,在论坛上别人是这么说的:
The "END" tag to your so-called "here" document (started by the <needs to be at the left hand side. That means it must be placed at the start of 
line without any whitespace in front of it. 

好了,按照上面的方法,改成如下的方式就可以了,高兴死了.......
<TRUNCATE TABLE ZHJS_TP_UNIQUE_ID;
exit
EOF