1、简单shell调用sqlplus
编写脚本
[oracle@localhost oracle_script]$ vimtest1.sh
#!/bin/bash
# user env
source ~/.bash_profile
sqlplus -S /nolog > result.log<<EOF
--set heading off feedback off pagesize 0verify off echo off
conn scott/tiger
select * from emp where empno= 7369;
exit
EOF
备注:这里注意要加上source ~/.bash_profile环境变量,否则找不到SQLPLUS命令,
或者直接用export ...... oracle路径也可
修改脚本权限
[oracle@localhost oracle_script]$ chmod +x test1.sh
[oracle@localhost oracle_script]$ ll
总计 4
-rwxr-xr-x 1 oracle oinstall 154 01-1308:59 test1.sh
执行脚本
[oracle@localhost oracle_script]$./test1.sh
查看结果
[oracle@localhost oracle_script]$ catresult.log
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
2、Sqlplus返回值给Shell(一)
方式一:用Shell的小板键` 来执行sqlplus:
[oracle@localhost oracle_script]$ vi test2.sh
#!/bin/bash
# user env
source ~/.bash_profile
VALUE=`sqlplus -S /nolog <<EOF
set heading off feedback off pagesize 0verify off echo off numwidth 4
conn scott/tiger
select count(*) from emp;
exit
EOF`
if [ "$VALUE" -gt 0 ]; then
echo "The number of rows is$VALUE."
exit 0
else
echo "There is no row in thetable."
fi
注意:等号两边不能有空格,有空格会报错如下:
[oracle@localhost oracle_script]$ ./test2.sh
./test2.sh: line 2: VALUE: command notfound
./test2.sh: line 8: [: : integer expressionexpected
There is no row in the table.
修改脚本权限
[oracle@localhost oracle_script]$ chmod +x test2.sh
[oracle@localhost oracle_script]$ ll
总计 4
-rwxr-xr-x 1 oracle oinstall 154 01-1308:59 test2.sh
执行查看结果
[oracle@localhost oracle_script]$ ./test2.sh
The number of rows is 14.
3、Sqlplus返回值给Shell(二)
该sqlplus使用 col 列名 new_value 变量名 定义了变量并带参数exit, 将变量v_coun返回赋给了shell的$?
[oracle@localhost oracle_script]$ cattest3.sh
#!/bin/bash
# user env
source ~/.bash_profile
sqlplus -S /nolog > result.log<<EOF
--set heading off feedback off pagesize 0verify off echo off numwidth 4
conn scott/tiger
col coun new_value v_coun
select count(*) coun from emp;
select * from emp where empno=7369;
exit v_coun
EOF
VALUE="$?"
echo "The number of rows is$VALUE."
备注:NEW_VALUE通常的使用方法为:
column column_name new_value var_name
new_value是将所获得的列值赋予到变量名,然后该变量名可以参与后续处理
修改脚本权限
[oracle@localhost oracle_script]$ chmod +x test3.sh
[oracle@localhost oracle_script]$ ll
总计 16
-rw-r--r-- 1 oracle oinstall 5 01-16 15:10 result.log
-rwxr-xr-x 1 oracle oinstall 213 01-1310:00 test1.sh
-rwxr-xr-x 1 oracle oinstall 285 01-1615:08 test2.sh
-rwxr-xr-x 1 oracle oinstall 259 01-1615:10 test3.sh
执行查看结果
[oracle@localhost oracle_script]$ ./test3.sh
The number of rows is 14.
4、shell传参给sqlplus
$1是传给SHELL的第一个参数,将参数传入给SQLPLUS , 变量赋值表达式的等号2边不能有空格.
[oracle@localhost oracle_script]$ vi test4.sh
#!/bin/bash
# user env
source ~/.bash_profile
NAME="$1"
sqlplus -S scott/tiger <<EOF
select * from emp where ename =upper('$NAME');
exit
EOF
修改脚本权限
[oracle@localhost oracle_script]$ chmod +x test4.sh
[oracle@localhost oracle_script]$ ll
总计 4
-rwxr-xr-x 1 oracle oinstall 154 01-1308:59 test4.sh
执行查看结果
[oracle@localhostoracle_script]$ ./test4.sh
no rows selected
[oracle@localhost oracle_script]$ ./test4.sh smith
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO -------- ---------- ------------------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
5、安全Shell调用Sqlplus
为了保证连接数据库安全,每次调用脚本的时候要输入用户名和密码
[oracle@localhost oracle_script]$ vi test5.sh
#!/bin/bash
# user env
source ~/.bash_profile
echo -n "Enter user name:"
read UNAME
echo -n "Enter password foruser:"
read PASSWD
sqlplus -S /nolog <<EOF
conn $UNAME/$PASSWD
select * from emp;
exit
EOF
[oracle@localhost oracle_script]$ ./test5.sh
Enter user name:scott
Enter password for user:tiger
EMPNO ENAME JOB MGR HIREDATE SAL
---------- ---------- --------- ------------------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800