一、最简单的shell里调用sqlplus.

$ vi test1.sh

#!/bin/bash
sqlplus -S /nolog > result.log <<EOF
set heading off feedback off pagesize 0 verify off echo off
conn u_test/iamwangnc
select * from tab;
exit
EOF

$ chmod +x test1.sh
$ ./test1.sh

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

注意sqlplus段使用老板键`了, 赋变量的等号两侧不能有空格.

$ vi test2.sh

#!/bin/bash
VALUE=`sqlplus -S /nolog <<EOF
set heading off feedback off pagesize 0 verify off echo off numwidth 4
conn u_test/iamwangnc
select count(*) from tab;
exit
EOF`
if [ "$VALUE" -gt 0 ]; then
        echo "The number of rows is $VALUE."
        exit 0
else
        echo "There is no row in the table."
fi

$ 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 verify off 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 u_test/iamwangnc <<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

--End--

最新文章

  1. vertx核心类之VertxImpl
  2. 【Alpha】Daily Scrum Meeting第一次
  3. Redis客户端连接池
  4. MySQL 配置文件中忘配置default-character-set引发的乱码问题
  5. nginx https性能优化
  6. str_replace() 用法bug和技巧
  7. 操作系统,windows编程,网络,socket
  8. Windows Store App 用户库文件操作
  9. LeetCode() Minimun Size Subarray Sum
  10. Android Service学习
  11. 汉诺塔问题C++实现
  12. Effective Java2读书笔记-创建和销毁对象(一)
  13. javascript 【封装AJAX】
  14. 属性动画(Property Animation)
  15. 自签名证书和私有CA签名的证书的区别 创建自签名证书 创建私有CA 证书类型 证书扩展名【转】
  16. UiPath针对SAP的输入技巧
  17. centos安装多个tomcat
  18. WPF 凭证分录控件
  19. django错误笔记(xadmin)——AttributeError: &#39;Settings&#39; object has no attribute &#39;TEMPLATE_CONTEXT_PROCESSORS&#39;
  20. 【python】time和datetime的strptime不是线程安全的!

热门文章

  1. 原始套接字--traceroute
  2. lowercase calligraphic letters
  3. C++寒假学习计划
  4. JDK从1.8升级到9.0.1后sun.misc.BASE64Decoder和sun.misc.BASE64Encoder不可用
  5. 51NOD 1554 欧姆诺姆和项链 巧妙利用KMP
  6. spring3创建RESTFul Web Service
  7. .ini配置CAN信息
  8. cms .net webform去服务器控件标签化 pagebase新版本
  9. HDU1083_Courses_课程_C++
  10. OpenGL函数思考-glColor