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. Redis' High Availability

    Redis Sentinel is a system designed to help managing Redis instances. It performs the following thre ...

  2. CSE(Corrupted State Exceptions) 严重异常处理办法

    原因分析 出现这个问题说明.NET版本至少是4.0,因为微软在.NET 4.0版本中更改了异常处理机制.微软认为catch(Exception)这种写法是不负责任的,程序员应该按照异常严重类别决定程序 ...

  3. PHP中的Trait

    Trait 自 PHP 5.4.0 起,PHP 实现了一种代码复用的方法,称为 trait. Trait 是为类似 PHP 的单继承语言而准备的一种代码复用机制.Trait 为了减少单继承语言的限制, ...

  4. 学习css简单内容

    Css的class,ID和上下文选择符 Class选择符. Class选择符用来配置某一类css规则,将其应用到网页中一个或多个区域.配置一类样式时,要将选择符配置成类名.在类名前加(.).类名必须以 ...

  5. nginx限制ip请求次数 以及并发次数

    如何设置能限制某个IP某一时间段的访问次数是一个让人头疼的问题,特别面对恶意的ddos攻击的时候.其中CC攻击(Challenge Collapsar)是DDOS(分布式拒绝服务)的一种,也是一种常见 ...

  6. C#中显/隐式实现接口及其访问方法

    原贴地址: http://www.cnblogs.com/dudu837/archive/2009/12/07/1618663.html 在实现接口的时候,VS提供了两个菜单,一个是"实现接 ...

  7. 三种方式得到LayoutInflater

    LayoutInflaterinflater=LayoutInflater.from(this); LayoutInflaterinflater=getLayoutInflater(); Layout ...

  8. iOS 之美:iOS Delegate 使用五步曲

    在iOS 开发中, 搞清楚Delegate 是需要花些时间的. Delegate 本来是软件架构设计的一种理念.对于像手机这样一个有限的设备,我们需要充分考虑到:内存要尽量省着用: 视图之间的关系要清 ...

  9. Android 全屏相关操作

    1.隐藏标题栏(titlebar) (1)在代码中隐藏标题栏 requestWindowFeature(Window.FEATURE_NO_TITLE); (2)在Manifest中Applicati ...

  10. 修改msconfig->引导->高级选项-》最大内存为512M

    本来想开机提速的!手贱  把 最大内存设置成了512M  结果开机悲剧了,启用了微软的自动修复也不能解决问题!最后是WIN7 PE系统下直接修复boot结果了.遇到这种问题的朋友们可以试试喔