2016-07-29 51 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:一元運算符預計 失敗 –

相關問題