我有很多shell腳本來執行Oracle數據庫上的過程和其他任務。通常,它們具有相同的結構,檢查envvars,執行sql並使用show sqlcode
來知道腳本是否成功執行。腳本不顯示sqlcode
這是代碼:
#!/bin/bash
# Check for var's in another script
. /home/oracle/cron/scripts/exp_variaveis.sh
$ORACLE_HOME/bin/sqlplus User/[email protected] <<!
spool /home/oracle/cron/log/some_name.log_$date_log
set serveroutput on
DECLARE
V_QTD NUMBER;
BEGIN
SELECT COUNT(*) QTD
INTO V_QTD
FROM D.TB_R_A TRA
WHERE TRA.CD_STATUS = 'A'
AND TRA.CD_T_AUT = 'E'
AND TRA.DT_P < TRUNC(SYSDATE);
IF V_QTD = 0 THEN
DBMS_OUTPUT.PUT_LINE('Sem resgates para serem expirados.');
ELSE
FOR R1 IN (SELECT TRA.CD_R, TRA.CD_A, TRA.CD_O
from D.TB_R_A TRA
WHERE TRA.CD_STATUS = 'A'
AND TRA.CD_T_AUT = 'E'
and TRA.DT_P < TRUNC(sysdate))
LOOP
BEGIN
UPDATE D.TB_R_A T
SET T.CD_STATUS = 'E'
WHERE T.CD_R = R1.CD_R;
COMMIT;
DBMS_OUTPUT.PUT_LINE('Resgate expirado: ' || R1.CD_R);
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE('Falha na expiracao do resgate: ' || R1.CD_R);
END;
END LOOP;
END IF;
END;
-- command show slqcode to know if was executed with success
show sqlcode
spool off
exit
!
# A variavel recebe a instrucao de busca do arquivo e se existir reporta o "sqlcode 0" que equilave a "SUCESSO"
sql_code=`grep -i "sqlcode 0" /home/oracle/cron/log/some_name.log_$date_log`
# A variavel trata a instrucao acima e processa os dados nos textos e operacoess com arquivos (awk) mostrando de forma CSV (-F)
# apresentando as strings da segunda coluna '{ print $2 }'
ret_code=`echo $sql_code | awk -F" " '{ print $2 }'`
# Se a variavel $ret_code for diferente de 0
if [ "$ret_code" != 0 ]
then
# Send error message
mail [email protected] -s "!! ERROR !! CRON => DATABASE - SOME TEXT HERE" < /home/oracle/cron/log/some_name.log_$date_log
exit 1
else
# Send sucessful message
mail [email protected] -s "Job OK CRON => DATABASE - SOME TEXTE HERE" < /home/oracle/cron/log/some_name.log_$date_log
exit 0
fi
我的問題是:這個SQL不輸出SQLCODE,所以如果我的腳本無法找到日誌字符串「SQLCODE」,它發送一個錯誤信息即使它成功執行。
請有人告訴我什麼是錯的。
事情是這樣的: 「END LOOP; END IF; END; \顯示SQLCODE閥芯關閉退出」? –
它應該是一個正斜槓,而不是反斜槓。添加到我的答案。 – trincot