shell如何调用sqlplus(各种情况示例)

时间:2024-10-13 16:30:55

1、最简单的shell里调用sqlplus.$ vi test1.sh#!/bin/bashsqlplus -S /nolog <<EOFset heading off feedback off pagesize 0 verify off echo offconn test/testselect * from tab;exitEOF$ chmod +x test1.sh$ ./test1.sh

shell如何调用sqlplus(各种情况示例)

2、把sqlplus执行结果传递给shell方法一$ vi test2.sh#!/bin/bashVALUE=`sqlplus -S /nolog <<E晦倘佳鳎OFset heading off feedback off pagesize 0 verify off echo off numwidth 4conn test/testselect count(*) from tab;exitEOF`if [ "$VALUE" -gt 0 ]; then echo "The number of rows is $VALUE." exit 0else echo "There is no row in the table."fi$ chmod +x test2.sh$ ./test2.sh

shell如何调用sqlplus(各种情况示例)

3、把sqlplus执行结果传递给shell方法二$ vi test3.sh#!/bin/bashsqlplus -S /nolog <<EOFset h髫潋啜缅eading off feedback off pagesize 0 verify off echo off numwidth 4conn test/testcol coun new_value v_counselect count(*) coun from tab;exit v_counEOFVALUE="$?"echo "The number of rows is $VALUE."$ chmod +x test3.sh$ ./test3.sh

shell如何调用sqlplus(各种情况示例)

4、把shell程序参数传递给sqlplus$1表示第一个参数, sqlplus里可以直接使用, 赋变量的等号两侧不能有空格不能有空格.$ vi test4.sh#!/bin/bashNAME="$1"sqlplus -S test/test <<EOFselect * from tab where tname = upper('$NAME');exitEOF$ chmod +x test4.sh$ ./test4.sh ttt

shell如何调用sqlplus(各种情况示例)

5、为了安全要求每次执行shell都手工输入密码$ vi test5.sh#!/bin/bashecho -n "Enter password for u_test:"read PASSWDsqlplus -S /nolog <<EOFconn test/$PASSWDselect * from tab;exitEOF$ chmod +x test5.sh$ ./test5.sh

shell如何调用sqlplus(各种情况示例)

6、为了安全从文件读取密码对密码文件设置权限, 只有用户自己才能读写.$ echo 'test' > u_test.txt$ ch罪焐芡拂mod g-rwx,o-rwx u_test.txt$ vi test6.sh#!/bin/bashPASSWD=`cat u_test.txt`sqlplus -S /nolog <<EOFconn test/$PASSWDselect * from tab;exitEOF$ chmod +x test6.sh$ ./test6.sh

shell如何调用sqlplus(各种情况示例)
© 手抄报圈