软讯网络 > 冲浪宝典 > 网络资源 > Connecting SQL*PLUS with a shell script
【标 题】:Connecting SQL*PLUS with a shell script
【关键字】:
Connecting,SQL,PLUS,with,shell,script
【来 源】:http://www.cublog.cn/u/14784/showart.php?id=276443
Connecting SQL*PLUS with a shell script
Here's a piece that allows to execute select statements on an Oracle database from a shell script. It consists of two parts: a shell function (read_sql_stmt) and the part that actually uses the function.
#!/bin/bash
read_sql_stmt() {
typeset stmt=$1
typeset login=$2
echo "
set feedback off verify off heading off pagesize 0
$stmt;
exit
" | sqlplus -s $login
}
The second part: read_sql_stmt is executed with an SQL statement and a login, its output is piped into an ordinary shell while loop:
read_sql_stmt "select username, user_id from dba_users" "system/system_pw" | while read u i
do
echo "user $u has userid $i"
done