shell 与 sqlplus 交互
来源:互联网 发布:守望先锋数据查询 编辑:程序博客网 时间:2024/06/12 21:53
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
- shell 与 sqlplus 交互
- shell 与 sqlplus 交互
- shell 与 sqlplus 交互
- shell 与 sqlplus 交互
- linux shell 与sqlplus交互
- Shell 与 SQLPLUS 的 交互 ---- 参数传递 返回值
- PHP与Shell交互
- [Python]与shell交互
- PHP与Shell交互
- sqlplus与shell互相传值
- sqlplus 与 shell 结合--shell中执行sql 脚本
- awk 与 shell 交互方式
- AWK----awk与shell交互
- Python与shell交互方式
- Shell 与Python的交互
- expect 实现与shell交互
- python 与ADB shell交互
- Shell(Bash) - PHP与Shell交互
- 第七周作业——背包问题
- OCP-1Z0-051 第45题 SEQUENCE(序列)的注意事项
- G++和C++ && POJ 1113 Wall
- c++-拷贝构造函数(复制构造函数)
- PHP基础学习第五讲(操纵数据库)
- shell 与 sqlplus 交互
- Linux软连接和硬链接
- 从零开始学HTML(五)--HTML 标题
- 输出最长字符串
- 理解Java泛型
- ZOJ-3204
- leetcode之Triangle
- 开辟内存空间
- ZOJ2750