shell 與 sqlplus 互動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
備忘:整理自互連網