记录日常工作关于系统运维,虚拟化云计算,数据库,网络安全等各方面问题。

shell调用sqlplus各种情况示例



shell调用sqlplus各种情况示例
测试平台:RHEL5.5
一、最简单的shell里调用sqlplus.
$ vi test1.sh

 #!/bin/bash
 sqlplus-s 
acctmp/acctmp@newtest>/dev/null<<EOF
 set serveroutput on;
 exec ngoss.PROC_LOAD3GGROUPUSER;
 exit;
 EOF
 
$ chmod +x test1.sh
$ ./test1.sh

二、把sqlplus执行结果传递给shell方法一

注意sqlplus段使用老板键`了, 赋变量的等号两侧不能有空格.
$ vi test2.sh
 
 #!/bin/bash
 #1) 优惠标识列字段为空
 preferencetypenull=`sqlplus-s 
acctmp/acctmp@newtest<<END
  set pagesize 0 feedback off verify off headingoff echo off
  SELECT COUNT (*) FROMinfo.t_ci_wocardinfo 

 WHERE  preferencetype IS NULL ANDTO_CHAR

   (insertdate, 'yyyymmdd') = TO_CHAR (SYSDATE,'yyyymmdd');
  exit;
  END`

 echo "本次csm所给沃家庭电子券优惠文件中优惠标识列为空总数为:"$preferencetypenull
#若本次csm所给沃家庭电子券优惠文件中优惠标识列为空总数不为0需要特殊处理:打印在日志中,并将其#删除
 if [ $preferencetypenull -ne 0 ]; then
#本次导入数据中存在优惠标识列字段为空的数据时要将其打印在日志中,并将其删除
 preferencetypenullsvcnums=`sqlplus-s 
acctmp/acctmp@newtest<<!
  set pagesize 0 feedback off verify off headingoff echo off
  SELECT svcnum
    FROMinfo.t_ci_wocardinfo
   WHERE preferencetype ISNULL
    AND TO_CHAR (insertdate,'yyyymmdd') = TO_CHAR (SYSDATE,'yyyymmdd');  
  exit;
  !`
  echo"优惠标识列字段为空的号码具体如下:"
  echo$preferencetypenullsvcnums

$ chmod +x test2.sh
$ ./test2.sh

三、把sqlplus执行结果传递给shell方法二
注意sqlplus段使用 col ..new_value .. 定义了变量并带参数exit, 然后自动赋给了shell的$?
$ vi test3.sh
 #!/bin/bash
  sqlplus -s /nolog > result.log<<EOF
  set heading off feedback off pagesize 0 verifyoff echo off  numwidth 4
  conn u_test/iamwangnc
  col coun new_value v_coun
  select count(*) coun from tab;
  exit v_coun
  EOF
  VALUE="$?"
  echo "The number of rows is$VALUE."

$ chmod +x test3.sh
$ ./test3.sh

四、把shell程序参数传递给sqlplus
$1表示第一个参数,sqlplus里可以直接使用,赋变量的等号两侧不能有空格不能有空格.
$ vi test4.sh
 #!/bin/bash
 NAME="$1"
 sqlplus-s 
acctmp/acctmp@newtest<<EOF
 select * from tab where tname =upper('$NAME');
 exit
 EOF

$ chmod +x test4.sh
$ ./test4.sh ttt

五、为了安全要求每次执行shell都手工输入密码
$ vi test5.sh

#!/bin/bash
echo -n "Enter password for u_test:"
read PASSWD
sqlplus -S /nolog <<EOF
conn u_test/$PASSWD
select * from tab;
exit
EOF

$ chmod +x test5.sh
$ ./test5.sh

六、为了安全从文件读取密码
对密码文件设置权限, 只有用户自己才能读写.

$ echo 'iamwangnc' > u_test.txt
$ chmod g-rwx,o-rwx u_test.txt
$ vi test6.sh

#!/bin/bash
PASSWD=`cat u_test.txt`
sqlplus -S /nolog <<EOF
conn u_test/$PASSWD
select * from tab;
exit
EOF

$ chmod +x test6.sh
$ ./test6.sh


转载请标明出处【shell调用sqlplus各种情况示例】。

《www.micoder.cc》 虚拟化云计算,系统运维,安全技术服务.

网站已经关闭评论