shell 与 sqlplus 交互
1、简单shell调用sqlplus
编写脚本
[oracle@localhost oracle_script]$
#!/bin/bash
# user env
source ~/.bash_profile
sqlplus -S /nolog > <<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
[oracle@localhost oracle_script]$ ll
总计 4
-rwxr-xr-x 1 oracle oinstall 154 01-1308:59
执行脚本
[oracle@localhost oracle_script]$./
查看结果
[oracle@localhost oracle_script]$
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
#!/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]$ ./
./: line 2: VALUE: command notfound
./: line 8: [: : integer expressionexpected
There is no row in the table.
修改脚本权限
[oracle@localhost oracle_script]$ chmod +x
[oracle@localhost oracle_script]$ ll
总计 4
-rwxr-xr-x 1 oracle oinstall 154 01-1308:59
执行查看结果
[oracle@localhost oracle_script]$ ./
The number of rows is 14.
3、Sqlplus返回值给Shell(二)
该sqlplus使用 col 列名 new_value 变量名 定义了变量并带参数exit, 将变量v_coun返回赋给了shell的$?
[oracle@localhost oracle_script]$
#!/bin/bash
# user env
source ~/.bash_profile
sqlplus -S /nolog > <<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
[oracle@localhost oracle_script]$ ll
总计 16
-rw-r--r-- 1 oracle oinstall 5 01-16 15:10
-rwxr-xr-x 1 oracle oinstall 213 01-1310:00
-rwxr-xr-x 1 oracle oinstall 285 01-1615:08
-rwxr-xr-x 1 oracle oinstall 259 01-1615:10
执行查看结果
[oracle@localhost oracle_script]$ ./
The number of rows is 14.
4、shell传参给sqlplus
$1是传给SHELL的第一个参数,将参数传入给SQLPLUS , 变量赋值表达式的等号2边不能有空格.
[oracle@localhost oracle_script]$ vi
#!/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
[oracle@localhost oracle_script]$ ll
总计 4
-rwxr-xr-x 1 oracle oinstall 154 01-1308:59
执行查看结果
[oracle@localhostoracle_script]$ ./
no rows selected
[oracle@localhost oracle_script]$ ./ 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
#!/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]$ ./
Enter user name:scott
Enter password for user:tiger
EMPNO ENAME JOB MGR HIREDATE SAL
---------- ---------- --------- ------------------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800
备注:整理自互联网