Unix/Linux下,shell脚本调用sqlplus的几种方式介绍:

一、最简单的shell调用sqlplus

#!/bin/bash
sqlplus -S /nolog > sqlplus.log <<EOF
   conn scott/scott
   select sysdate from dual;
   quit
EOF

二、sqlplus返回执行结果给shell

方法一:

#!/bin/bash
biz_date=`sqlplus -S scott/scott <<EOF
set heading off
set pagesize 0;
set feedback off;
set verify off;
set echo off;
   select sysdate from dual;
   exit
EOF`
echo $biz_date

(注意:等号两侧不能有空格.)

[oracle@toughhou shell]$ vi sqlplus.sh
21-NOV-13

方法二:

注意sqlplus段使用 col .. new_value .. 定义了变量并带参数exit, 然后自动赋给了shell的$?

#!/bin/bash
sqlplus -S scott/scott <<EOF
set heading off
set pagesize 0;
set feedback off;
set verify off;
set echo off;
   col biz_date new_value v_biz_date
   select sysdate biz_date from dual;
   exit v_biz_date
EOF 
biz_date="$?"

[oracle@toughhou shell]$ vi sqlplus.sh
sqlplus.sh: line 11: warning: here-document at line 1 delimited by end-of-file (wanted `EOF')
21-NOV-13
这里出warning是因为EOF后面有空格。(注意:结尾出的EOF后面不能有任何字符)

去掉空格后结果如下:
[oracle@toughhou shell]$ vi sqlplus.sh
22-NOV-13

三、shell程序传递参数给sqlplus
sqlplus里可以直接使用, 赋变量的等号两侧不能有空格不能有空格.
接收到的变量不能加引号。“select sysdate from $tb;"不能写成"select sysdate from '$tb';"

#!/bin/bash
tb=dual

sqlplus -S scott/scott <<EOF
set heading off
set pagesize 0;
set feedback off;
set verify off;
set echo off;
   select sysdate from $tb;
   exit
EOF

[oracle@toughhou shell]$ sh sqlplus.sh 
22-NOV-13

四、为了安全要求每次执行shell都手工输入密码

#!/bin/bash
echo -n "Enter db password for scott: "
read pwd

sqlplus -S scott/$pwd <<EOF
set heading off
set pagesize 0;
set feedback off;
set verify off;
set echo off;
   select sysdate from dual;
   exit
EOF

[oracle@toughhou shell]$ sh sqlplus.sh 
Enter db password for scott: scott
22-NOV-13

五、为了安全从文件读取密码
对密码文件设置权限, 只有用户自己才能读写.
[oracle@toughhou shell]$ echo scott > scott.pwd
[oracle@toughhou shell]$ chmod 500 soctt.pwd
[oracle@toughhou shell]$ chmod 500 scott.pwd
[oracle@toughhou shell]$ ls -l scott.pwd 
-r-x------ 1 oracle oinstall 6 Nov 22 00:17 scott.pwd

#!/bin/bash
pwd=`cat scott.pwd`

sqlplus -S scott/$pwd <<EOF
set heading off
set pagesize 0;
set feedback off;
set verify off;
set echo off;
   select sysdate from dual;
   exit
EOF

Unix/Linux中shell调用sqlplus的方式的更多相关文章

  1. linux中shell变量$#,$@,$0,$1,$2的含义

    linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...

  2. Linux中Shell

    Linux中Shell Shell是什么 ​ Shell是一个命令行解释器,为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,可以用Shell来启动.挂起.停止.编写一些程序. S ...

  3. linux中Shell标准输出错误 >/dev/null 2>&1 分析【转】

    Shell中可能经常能看到:>/dev/null  2>&1 eg:sudo kill -9 `ps -elf |grep -v grep|grep $1|awk '{print ...

  4. [转]unix/linux中的dup()系统调用

    [转]unix/linux中的dup()系统调用    在linux纷繁复杂的内核代码中,sys_dup()的代码也许称得上是最简单的之一了,但是就是这么一个简单的系统调用,却成就了unix/linu ...

  5. linux中shell变量$#,$@,$0,$1,$2的含义解释

    linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...

  6. ASP.net 中手工调用WS(POST方式)

    ASP.net 中手工调用WS(POST方式)核心代码:string strUrl="http://localhost:21695/service1.asmx/getmythmod" ...

  7. linux中shell变量$#,$@,$0,$1,$2的含义解释

    linux中shell变量$#,$@,$0,$1,$2的含义解释 linux中shell变量$#,$@,$0,$1,$2的含义解释:  变量说明:  $$  Shell本身的PID(ProcessID ...

  8. Linux中shell变量$0,$?等含义

    linux中shell变量$#,$@,$0,$1,$2的基本含义: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...

  9. 【转】linux中shell变量$#,$@,$0,$1,$2的含义解释

    原文网址:http://www.cnblogs.com/fhefh/archive/2011/04/15/2017613.html linux中shell变量$#,$@,$0,$1,$2的含义解释: ...

随机推荐

  1. 给jdk写注释系列之jdk1.6容器(11)-Queue之ArrayDeque源码解析

    前面讲了Stack是一种先进后出的数据结构:栈,那么对应的Queue是一种先进先出(First In First Out)的数据结构:队列.      对比一下Stack,Queue是一种先进先出的容 ...

  2. Flex性能优化常用手法总结 转

    转自:http://bbs.51aspx.com/showtopic-43693.html 随着Flex越来越多的被人们所熟知,越来越多的互联网也开始了RIA应用.众所周知,目前国内的宽带应用并不是像 ...

  3. Math.random获得随机数

    function GetRandomNum(Min,Max){           var Range = Max - Min;           var Rand = Math.random(); ...

  4. 再次阅读《精通CSS-高级Web标准解决方案(第二版)》

    昨天(2015年11月21日) 在我们学校举行了大型招聘会.我面试了三家企业.有一家企业是先做笔试题的,做完后发现自己还是很多细节处理得不够.无论还有没有二面,我还是要重新把<精通CSS> ...

  5. ajax开发模拟后端数据接口

    在做前端开发的时候,特别是一些业务逻辑集中在前端的开发中.我们经常需要自己来模拟获取到后台接口的数据.为什么要模拟?可能后台接口还没有开发完成,可能后台还没有数据返回,可能...等等原因.曾经,我也尝 ...

  6. Jersey(1.19.1) - Conditional GETs and Returning 304 (Not Modified) Responses

    Conditional GETs are a great way to reduce bandwidth, and potentially server-side performance, depen ...

  7. MyBatis(3.2.3) - Handling the CLOB/BLOB types

    MyBatis provides built-in support for mapping CLOB/BLOB type columns. Assume we have the following t ...

  8. 上传系列:ajaxupload.js

    ajaxupload.js 上次说了jquery.upload.js,这次再说一下ajaxupload.js,这个其实也比较简答,只有一个JS文件: html代码: $(function () { v ...

  9. 【转】Log4.NET mark

    C#通过log4net进行异常记录 C#中异常的记录也有一个模板,就是log4net.多的就不说了直接看怎么用的吧. 1.引用log4net.dll. 2.实现log4net的方法. public c ...

  10. 转(C#)Winform中MD5加密

    MD5的全称是message-digest algorithm 5(信息-摘要算法,在90年代初由mit laboratory for computer science和rsa data securi ...