2016-07-29 45 views
0

我是shell脚本的新手。 我正在写一个DBA自动化的代码,其中我有master.sql文件有n个sqls的声明。这些sqls被放置在特定文件夹内。 我有一个脚本,调用此master.sql和一次间接执行的sql语句,并在一次 wwhat我想在如何在任何sql失败时停止unix shell脚本中的sqlplus命令

sqlplus的命令是节省内部master.sql声明的所有SQL的输出的日志文件保存是否它必须停止执行时,第一个sql失败,并返回错误与特定的SQL失败或要求回滚,并要求你想继续或不? 每次尝试使用sqlplus,sqlerror,$?毫无效果

sqlplus ${USER}/${PASS}@$SID @master.sql > /home/deploy/vidya/properties/result/result.log 

现在我所试图做的是....我希望在任何的sql从起点失败,提示有错误的命令行上停止sql语句的执行和指定SQL失败

master.sql

SET ECHO ON 

@/home/deploy/vidya/properties/001_dba_demo.sql 
@/home/deploy/vidya/properties/003_dba_demo1.sql 
@/home/deploy/vidya/properties/002_dba_demo2.sql 
@/home/deploy/vidya/properties/004_dba_demo2.sql 

EXIT 

这是调用master.sql其正常工作

我.SH脚本
#!/bin/bash 

echo "Enter schema Name you want to connect: " 
read USER 
echo "Enter password: " 
read -s PASS 
#echo "Enter Oracle SID for the environment you want to connect with : " 
#read SID 
echo "Enter Environment for the environment you want to connect with : " 
read ENV 
echo "Enter Oracle SID for the environment you want to connect with : " 
read SID 

export ORACLE_SID=${SID} 

if [ $ENV == UAT ] 
then 
    echo "Connected withh UAT ORACLE HOME:" 
export ORACLE_HOME=/u01/app/oracle/product/11.2.0.4/db_1 
elif [ $ENV == PROD ] 
then 
echo "Connected with PROD ORACLE HOME:" 
export ORACLE_HOME=/u01/app/oracle/product/11.2.0.4/db_1 

else 
echo "Wrong Environment Entered :" 

fi 
export PATH=$ORACLE_HOME/bin:$PATH 

echo "want to execute sql? y/n" 
read yn 

sqlplus ${USER}/${PASS}@$SID @master.sql > /home/deploy/vidya/properties/result/result.log 

**

谁能帮**

+1

[WHENEVER SQLERROR(HTTPS:/ /docs.oracle.com/cd/B19306_01/server.102/b14357/ch12052.htm)? – 7171u

回答

0

一个IDEEA将启动序列中的SQL的事后检查他们的错误:? 例如

sqlplus ${USER}/${PASS}@$SID @dba_demo.sql > /home/deploy/vidya/properties/result/result_dba_demoSQL.log 
checker = $(grep "ERROR" result_dba_demoSQL.log) 
if [ test -z $checker ] 
    then echo "dba_demo_SQL successfull run" 
    else 
     echo "dba_demo_SQL failed" 
     exit 0 
fi 

退出0部分,当grep在日志文件中发现错误时退出整个脚本。

+0

如果您不想在脚本中编写太多的if,可以将整个内容放入for:for对于'ls -1/some_location/folder_with_sqls'中的f;做the_if_part_above;完成。你可以在'for f in ...'中将f用作启动某些脚本的变量:sqlplus $ {USER}/$ {PASS} @ $ SID @ $ f并将输出保存到某个日志文件:>/home /部署/ vidya/properties/result/result_ $ f.log,并修改检查变量$(grep“ERROR”result_ $ f.log –

+0

sql失败应该被视为错误。将错误消息打印到stderr,并以非零值退出:'... else;回声“dba_demo_SQL失败”>&2;出口1; ' –

+0

同意William,更好地使用exit 1 –

0

我猜sqlplus具有一次执行sqls并将其保存在日志文件中的主要功能。它已经在做。我想要的是执行每一个sql一个接一个,如果有任何SQL失败,那么它应该停止执行,并在命令行上给出错误

0

对不起,不使用注释。没有足够的声誉。 这是我在第一个答案中的解决方案。 不是通过sqlplus启动master.sql,而是逐个启动较小的sqls,并在每次执行后检查日志文件。 如果sql编号1的日志文件有错误,if子句中的出口1将终止整个脚本。

#First query 
    #Sqlplus command to run the first query 
    sqlplus ${USER}/${PASS}@$SID @dba_demo.sql > /home/deploy/vidya/properties/result/result_dba_demoSQL.log 
    #variable checker is assigned ERROR, if ERROR is found in file (you have to check what errors you can get from oracle) 
    checker = $(grep "ERROR" result_dba_demoSQL.log) 
    #If clause that checks if variable checker is empty or not, if variable checker is empty, it means that no string "ERROR" was found in log files, thus script continues to run; If variable is not empty (string ERROR was chatched) script terminates. 
    if [ test -z $checker ] 
     then echo "dba_demo_SQL successfull run" 
     else 
      echo "dba_demo_SQL failed" 
      exit 1 
    fi 
    # Second Query 
    sqlplus ${USER}/${PASS}@$SID @dba_demo1.sql > /home/deploy/vidya/properties/result/result_dba_demo1SQL.log 
    checker = $(grep "ERROR" result_dba_demo1SQL.log) 
    if [ test -z $checker ] 
     then echo "dba_demo1_SQL successfull run" 
     else 
      echo "dba_demo1_SQL failed" 
      exit 1 
    fi 
+0

错误这是很好的解决方案,但事情是我做DBA自动化和杜安这对于客户端,以便它的要求。他们向我们发送master.sql文件,声明所有sqls。所以我们必须执行它。 。那么你能帮助使用这种方法吗? –

0

如果您master.sql是一样的,你张贴的例子,这意味着路径到SQL文件本身的列表,你可以这样做:

# Read the master.sql file, for every line in master.sql file 
    for i in `cat master.sql`; do 
    # basename "$i" takes the name of the file from path present in master.sql 
    sql_name = $(basename "$i") 
    # use path in the sqlplus command ($i from the for loop) and sqlname for the log file name 
    sqlplus ${USER}/${PASS}@$SID @$i > /home/deploy/vidya/properties/result/result_$sql_name.log 
checker = $(grep "ERROR" result_$sql_name.log) 
if [ test -z $checker ] 
    then echo "$sql_name successfull run" 
    else 
     echo "$sql_name failed" 
     exit 1 
fi ; done 
+0

error:./run-sqlplus2.sh:line 96:=:command not found exit grep:.log:No such file or directory ./run-sqlplus2.sh:line 99:checker:command not found ./run-sqlplus2.sh:第100行:[:test:一元运算符预计 失败 –

相关问题