shell动态脚本和pl/sql动态脚本的比较

简介: 最近项目有一个需求,需要在多个数据库的schema上跑一些脚本。希望dba能够提供一个脚本,能够根据需求在环境中执行指定的脚本。 乍一听,没什么技术难点,为了更明白的说明问题,我举个例子。

最近项目有一个需求,需要在多个数据库的schema上跑一些脚本。希望dba能够提供一个脚本,能够根据需求在环境中执行指定的脚本。
乍一听,没什么技术难点,为了更明白的说明问题,我举个例子。
有4个DB Instance: DB1,DB2,DB3,DB4
有6个DB Schemas, 5个table,分布如下: 
                         db schemas        tables
                          user1@DB1        table1, table2, table3, table4,table5
                          user2@DB2        table1, table2, table3, table4,table5
                          user3@DB3        table1, table3
                          user4@DB4        table1, table4
                          user5@DB1        table1, table5
                          user6@DB2        table2, table3, table4
实现的目标如下,对于同时含有table1--5的db schema才需要执行指定的脚本,脚本内容都是些dml操作。
目前的情况只能够得到db schema的列表,对于里面是否还有5个表,还没有细粒度的管理。
脚本需要从db schema的列表中筛选出符合的 db schema,然后执行脚本内容。


################# pl/sql执行情况 #############################

#!/bin/ksh
export ScriptName=`basename $0`
export ScriptDir=`dirname $0`
echo $ScriptName
echo $ScriptDir
rep_conn=$1
Env_Code=$2
sqlplus -s ${rep_conn} set serveroutput on
set feedback off
spool app_change_tmp.log
declare
conn_str   varchar2(100);
target_conn varchar2(200);
tmp_cnts number;
cursor cur_conn_strs is
select distinct ''||username||'/'||password||'@'||db_instance||''   conn_str  from xxxx   --查询制定的配置表,从里面得到一个基本的db schema列表
group by username,password,db_instance ;
begin
for cur_conn_str in cur_conn_strs  loop
dbms_output.put_line('conn '||cur_conn_str.conn_str);
dbms_output.put_line('set serveroutput on');
dbms_output.put_line('set feedback on');
dbms_output.put_line('set echo on');
dbms_output.put_line('declare');
dbms_output.put_line('tmp_cnt number;');
dbms_output.put_line('begin');
dbms_output.put_line('select count(*) into tmp_cnt from user_synonyms where synonym_name');
dbms_output.put_line(' in('||chr(39)||'T1'||chr(39)||','||chr(39)||'T2'||chr(39)||','||chr(39)||'T3'||chr(39)||','||chr(39)||'T4'||chr(39)||','||chr(39)||'T5'||chr(39)||');');
dbms_output.put_line('dbms_output.put_line(tmp_cnt);');
dbms_output.put_line('if(tmp_cnt>=5) then ');
dbms_output.put_line('dbms_output.put_line('||chr(39)||'app POST SCRIPTS RUNNING...'||chr(39)||');');
dbms_output.put_line('@script/script1.ps ');
dbms_output.put_line('@script/script2.ps ');
dbms_output.put_line('@script/script3.ps ');
dbms_output.put_line('dbms_output.put_line('||chr(39)||'app POST SCRIPTS RUNNING...'||chr(39)||');');
dbms_output.put_line('end if;');
dbms_output.put_line('end;');
dbms_output.put_line('/');
dbms_output.put_line(tmp_cnts);
end loop;
end;
/
spool off;
@app_change_tmp.log
EOS


如上的Pl/sql生成的动态pl/sql如下, 先判断是否还有T1--T5,如果条数符合,就执行脚本内容,但是有个限制就是执行脚本的时候如果脚本中有“set linesize... set define off之类的设置的话,脚本是运行不了的,对于ddl的执行也有一些限制。

################# 生成的动态 pl/sql 如下 #############################

conn user1/user1@DB1
set serveroutput on
set feedback on
set echo on
declare
tmp_cnt number;
begin
select count(*) into tmp_cnt from user_tables where table_name
in('T1','T2','T3','T4','T5');
dbms_output.put_line(tmp_cnt);
if(tmp_cnt>=5) then
dbms_output.put_line('app POST SCRIPTS RUNNING...');
@script/script1.ps
@script/script2.ps
@script/script3.ps
dbms_output.put_line('app POST SCRIPTS RUNNING...');
end if;
end;
/


conn user2/user2@DB1
set serveroutput on
set feedback on
set echo on
declare
tmp_cnt number;
begin
select count(*) into tmp_cnt from user_tables where table_name
in('T1','T2','T3','T4','T5');
dbms_output.put_line(tmp_cnt);
if(tmp_cnt>=5) then
dbms_output.put_line('app POST SCRIPTS RUNNING...');
@script/script1.ps
@script/script2.ps
@script/script3.ps
dbms_output.put_line('app POST SCRIPTS RUNNING...');

end if;
end;
/

################# pl/sql执行情况 #############################


############## shell 脚本实现动态shell ################################

echo 'app CHANGE START....'
cat $ScriptDir/script1.ps > $ScriptDir/app_all.ps
cat $ScriptDir/script2.ps >> $ScriptDir/app_all.ps
cat $ScriptDir/script3.ps>> $ScriptDir/app_all.ps
echo `sqlplus -s  ${rep_conn} set feedback off  pages 0
select distinct ''||username||'/'||password||'@'||db_instance||''   conn_str  from xxxxxx   --查询制定的配置表,从里面得到一个基本的db schema列表
group by username,password,db_instance ;
EOF`|awk  '{for (i=1;i print  "set feedback off pages 0";
print "select (case when (select count(*) from user_synonyms where synonym_name in ('\''T5'\'','\''T1'\'','\''T2'\'','\''T3'\'','\''T4'\''))>=5 then  '\''Y @app_all.ps'\'' else '\''N no_need_to_run_app_script'\'' end) from dual; ";print "EOS` " $i}}'> $ScriptDir/dynamic_tmp.ksh
ksh $ScriptDir/dynamic_tmp.ksh |awk '{ if( $1 =="Y" ){ print "sqlplus -s " $3 " $ScriptDir/app_change_tmp.ksh
ksh $ScriptDir/app_change_tmp.ksh
rm $ScriptDir/dynamic_tmp.ksh
echo 'app CHANGE ENDED....'
rm $ScriptDir/app_change_tmp.ksh


#################生成的动态shell脚本1内容如下#################

echo `sqlplus -s user1/user1@DB1  set feedback off pages 0
select (case when (select count(*) from user_tables where table_name in in('T1','T2','T3','T4','T5');)>=5 then  'Y @adj_all.ps' else 'N no_need_to_run_adj_script' end) from dual;
EOS`  user1/user1@DB1

echo `sqlplus -s  user2/user2@DB2  set feedback off pages 0
select (case when (select count(*) from user_tables where table_name in in('T1','T2','T3','T4','T5');)>=5 then  'Y @adj_all.ps' else 'N no_need_to_run_adj_script' end) from dual;
EOS` user2/user2@DB2

#################执行动态shell脚本1后生成的脚本2内容如下#################

sqlplus -s user1/user1@DB1 @adj_all.ps
EOS
sqlplus -s user2/user2@DB2 @adj_all.ps
EOS

############## shell 脚本实现动态shell ################################

目录
相关文章
|
8月前
|
存储 安全 Unix
七、Linux Shell 与脚本基础
别再一遍遍地敲重复的命令了,把它们写进Shell脚本,就能一键搞定。脚本本质上就是个存着一堆命令的文本文件,但要让它“活”起来,有几个关键点:文件开头最好用#!/usr/bin/env bash来指定解释器,并用chmod +x给它执行权限。执行时也有讲究:./script.sh是在一个新“房间”(子Shell)里跑,不影响你;而source script.sh是在当前“房间”里跑,适合用来加载环境变量和配置文件。
775 9
|
8月前
|
存储 Shell Linux
八、Linux Shell 脚本:变量与字符串
Shell脚本里的变量就像一个个贴着标签的“箱子”。装东西(赋值)时,=两边千万不能有空格。用单引号''装进去的东西会原封不动,用双引号""则会让里面的$变量先“变身”再装箱。默认箱子只能在当前“房间”(Shell进程)用,想让隔壁房间(子进程)也能看到,就得给箱子盖个export的“出口”戳。此外,Shell还自带了$?(上条命令的成绩单)和$1(别人递进来的第一个包裹)等许多特殊箱子,非常有用。
795 2
|
关系型数据库 MySQL Shell
MySQL 备份 Shell 脚本:支持远程同步与阿里云 OSS 备份
一款自动化 MySQL 备份 Shell 脚本,支持本地存储、远程服务器同步(SSH+rsync)、阿里云 OSS 备份,并自动清理过期备份。适用于数据库管理员和开发者,帮助确保数据安全。
|
11月前
|
Shell
Shell脚本循环控制:shift、continue、break、exit指令
使用这些命令可以让你的Shell脚本像有生命一样动起来。正确使用它们,你的脚本就能像一场精心编排的舞蹈剧目,既有旋律的起伏,也有节奏的跳跃,最终以一场惊艳的表演结束。每一个动作、每一个转折点,都准确、优雅地完成所需要表达的逻辑。如此,你的脚本不只是冰冷的代码,它透过终端的界面,跳着有节奏的舞蹈,走进观众——使用者的心中。
384 60
|
8月前
|
数据采集 监控 Shell
无需Python:Shell脚本如何成为你的自动化爬虫引擎?
Shell脚本利用curl/wget发起请求,结合文本处理工具构建轻量级爬虫,支持并行加速、定时任务、增量抓取及分布式部署。通过随机UA、异常重试等优化提升稳定性,适用于日志监控、价格追踪等场景。相比Python,具备启动快、资源占用低的优势,适合嵌入式或老旧服务器环境,复杂任务可结合Python实现混合编程。
|
10月前
|
Web App开发 缓存 安全
Linux一键清理系统垃圾:释放30GB空间的Shell脚本实战​
这篇博客介绍了一个实用的Linux系统盘清理脚本,主要功能包括: 安全权限检查和旧内核清理,保留当前使用内核 7天以上日志文件清理和系统日志压缩 浏览器缓存(Chrome/Firefox)、APT缓存、临时文件清理 智能清理Snap旧版本和Docker无用数据 提供磁盘空间使用前后对比和大文件查找功能 脚本采用交互式设计确保安全性,适合定期维护开发环境、服务器和个人电脑。文章详细解析了脚本的关键功能代码,并给出了使用建议。完整脚本已开源,用户可根据需求自定义调整清理策略。
1189 1
|
12月前
|
存储 Unix Shell
确定Shell脚本在操作系统中的具体位置方法。
这对于掌握Linux的文件系统组织结构和路径方面的理解很有帮助,是我们日常工作和学习中都可能使用到的知识。以上讲解详细清晰,应用简便,是每一个想要精通操作系统的计算机爱好者必备的实用技能。
636 17
|
Linux Shell
在Linux、CentOS7中设置shell脚本开机自启动服务
以上就是在CentOS 7中设置shell脚本开机自启动服务的全部步骤。希望这个指南能帮助你更好地管理你的Linux系统。
1939 25
|
Linux Shell
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
Centos或Linux编写一键式Shell脚本删除用户、组指导手册
371 4
|
Linux Shell 数据安全/隐私保护
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
Centos或Linux编写一键式Shell脚本创建用户、组、目录分配权限指导手册
631 3