Unix/Linux中shell调用sqlplus的方式
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=dualsqlplus -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 pwdsqlplus -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的方式的更多相关文章
- linux中shell变量$#,$@,$0,$1,$2的含义
linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...
- Linux中Shell
Linux中Shell Shell是什么 Shell是一个命令行解释器,为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,可以用Shell来启动.挂起.停止.编写一些程序. S ...
- linux中Shell标准输出错误 >/dev/null 2>&1 分析【转】
Shell中可能经常能看到:>/dev/null 2>&1 eg:sudo kill -9 `ps -elf |grep -v grep|grep $1|awk '{print ...
- [转]unix/linux中的dup()系统调用
[转]unix/linux中的dup()系统调用 在linux纷繁复杂的内核代码中,sys_dup()的代码也许称得上是最简单的之一了,但是就是这么一个简单的系统调用,却成就了unix/linu ...
- linux中shell变量$#,$@,$0,$1,$2的含义解释
linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...
- ASP.net 中手工调用WS(POST方式)
ASP.net 中手工调用WS(POST方式)核心代码:string strUrl="http://localhost:21695/service1.asmx/getmythmod" ...
- linux中shell变量$#,$@,$0,$1,$2的含义解释
linux中shell变量$#,$@,$0,$1,$2的含义解释 linux中shell变量$#,$@,$0,$1,$2的含义解释: 变量说明: $$ Shell本身的PID(ProcessID ...
- Linux中shell变量$0,$?等含义
linux中shell变量$#,$@,$0,$1,$2的基本含义: 变量说明: $$ Shell本身的PID(ProcessID) $! Shell最后运行的后台Process的PID $? 最后运行 ...
- 【转】linux中shell变量$#,$@,$0,$1,$2的含义解释
原文网址:http://www.cnblogs.com/fhefh/archive/2011/04/15/2017613.html linux中shell变量$#,$@,$0,$1,$2的含义解释: ...
随机推荐
- CF Playing with Paper
Playing with Paper time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 关于Linux系统和Windows系统中文件夹的命名规范
Windows系统中. 1.在创建文件夹的时候不能以"."开头(但是文件以多个点开头并且还有其他合法字符的话就是合法的) 但是在windows系统中确实见过以一个点".& ...
- #if...#endif的用法总结
在写到 pwm音阶程序的时候,在代码中有#if...#endif的用法问题,相关音阶的代码如下: /************************************************* ...
- spring+mybatis管理多个数据源(非分布式事务)
本文通过一个demo,介绍如何使用spring+mybatis管理多个数据源,注意,本文的事务管理并非之前博文介绍的分布式事务. 这个demo将使用两个事务管理器分别管理两个数据源.对于每一个独立的事 ...
- HTTP 错误 500.21 - Internal Server Error 处理程序“PageHandlerFactory-Integr
将网站发布到IIS,访问发生如下错误: HTTP 错误 500.21 - Internal Server Error处理程序"PageHandlerFactory-Integr"在 ...
- ios开发入门篇(四):UIWebView结合UISearchBar的简单用法
UIWebView是ios开发中比较常用的一个控件.我们可以用它来浏览网页.打开文档等,今天笔者在这里简单介绍下UIWebView和UISearchBar结合起来的用法,做一个简单的类浏览器. 一: ...
- MLlearning(1)——kNN算法
这篇文章讲kNN(k近邻,k-Nearest Neighbour).这是一种lazy-learning,实现方便,很常用的分类方法.约定n为样本集中的样本数,m为样本的维度,则这个算法的训练复杂度为0 ...
- OC4_实例变量的作用域
// // Dog.h // OC4_实例变量的作用域 // // Created by zhangxueming on 15/6/16. // Copyright (c) 2015年 zhangxu ...
- 当html中存在url中如: onclick="toView('参数1')", 参数1是特别字符,如&asop;"' "等时,浏览器解析时会报错。解决方法如文中描述
解决方案: 自定义标签将字符串转换成unicode编码后输出显示到页面即可 解析原理:解析顺序html ---url ----javascript---url,由于unicode编码在htm解析阶段 ...
- 项目中的那些事---PHP函数
总结工作中遇到的php函数: 1.查找:strpos("str", "substr"): 查找substr字符串在str字符串中出现的位置 第一个参数是:被查找 ...