sqlplus 查询 + 在 linux 上循环
sqlplus query + loop on linux
我在 unix 上的经验几乎为 0,在 mysql 上的经验很少,我发现很难从哪里开始完成这项任务,我必须编写一个 .sh 文件来每天启动一次。
我在 OracleDb 上有一个 table,其中我需要 2 列:路径和 file_name。
SELECT path, file_name FROM table WHERE date=trunc(sysdate);
一旦我得到结果,我应该循环遍历文件系统以检查其路径中的文件是否存在,
如果没有,脚本应该休眠几分钟并从查询中重复。
如果它确实找到了每个文件,那么它就完成了。
我!
我用这个递归函数解决了这个问题
execute_check(){
SEC_TO_WAIT=300
NUM_SEC=
if [ "NUM_SEC${NUM_SEC}" = "NUM_SEC" ]; then
echo "Not waiting"
else
echo "Wait "${NUM_SEC}" seconds"
sleep ${NUM_SEC}
fi
echo "===========================================" 2>&1 | tee -a ${LOG_FILE}
echo "= TRY QUERY ON DB =" 2>&1 | tee -a ${LOG_FILE}
echo "===========================================" 2>&1 | tee -a ${LOG_FILE}
sqlplus -S ${USERSQL}/${PWDSQL}@${DB} > ${SPOOL_FAKE} << EOF
WHENEVER SQLERROR EXIT 1
SET SERVEROUTPUT ON SIZE 1000000;
SET ARRAYSIZE 1;
SET LINESIZE 4000;
SET HEADING OFF;
SET TRIMSPOOL ON;
SET TIMING OFF;
spool ${FLUSSO_ELAB};
select path, file_name
from table
where date=trunc(sysdate);
spool off;
EOF
RESULT=$?
echo "Query result" $RESULT
if [ ${RESULT} = 0 ]; then
echo "===========================================" 2>&1 | tee -a ${LOG_FILE}
echo "= CHECKING FILES EXISTENCE =" 2>&1 | tee -a ${LOG_FILE}
echo "===========================================" 2>&1 | tee -a ${LOG_FILE}
else
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 2>&1 | tee -a ${LOG_FILE}
echo "! Error: !" 2>&1 | tee -a ${LOG_FILE}
echo "! error in the query to DB !" 2>&1 | tee -a ${LOG_FILE}
echo "! Exit code: ${RESULT} !" 2>&1 | tee -a ${LOG_FILE}
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 2>&1 | tee -a ${LOG_FILE}
exit 1
fi
# remove first and last row before the loop since they are empty
sed 1d ${SPOOL_FAKE} > ${FLUSSO_ELAB}
rm ${SPOOL_FAKE}
mv ${FLUSSO_ELAB} ${SPOOL_FAKE}
sed '$d' ${SPOOL_FAKE} > ${FLUSSO_ELAB}
rm ${SPOOL_FAKE}
mv ${FLUSSO_ELAB} ${SPOOL_FAKE}
#trasform whitespace in /
sed -i 's/ /\//g' ${SPOOL_FAKE}
while [ `wc -l ${SPOOL_FAKE} | awk -F " " '{print }'` -ge 1 ]
do
echo "-----------------------------------------------------------------------------------------------"
FILE=`head -1 ${SPOOL_FAKE}`
FILE=${FILES_DIR}/${FILE}
echo "Search for file: "${FILE} 2>&1 | tee -a ${LOG_FILE}
if [ -f ${FILE} ]; then
echo "File: "${FILE}" exists." 2>&1 | tee -a ${LOG_FILE}
else
echo "File: "${FILE}" does NOT exist." 2>&1 | tee -a ${LOG_FILE}
execute_check ${SEC_TO_WAIT}
fi;
# ----------------------------------------------------------------------------------------------
# DELETE FIRST ROW TO MOVE FORWARD THE LOOP
# ----------------------------------------------------------------------------------------------
sed 1d ${SPOOL_FAKE} > ${FLUSSO_ELAB}
rm ${SPOOL_FAKE}
mv ${FLUSSO_ELAB} ${SPOOL_FAKE}
done
}
在另一个 .sh 中调用,您在其中初始化了变量
FILES_DIR=${ROOT}/files
SPOOL_FAKE=${ROOT}/log/SpoolFake.txt
FLUSSO_ELAB=${ROOT}/log/FlussoElab.txt
execute_check 0
我在 unix 上的经验几乎为 0,在 mysql 上的经验很少,我发现很难从哪里开始完成这项任务,我必须编写一个 .sh 文件来每天启动一次。
我在 OracleDb 上有一个 table,其中我需要 2 列:路径和 file_name。
SELECT path, file_name FROM table WHERE date=trunc(sysdate);
一旦我得到结果,我应该循环遍历文件系统以检查其路径中的文件是否存在, 如果没有,脚本应该休眠几分钟并从查询中重复。
如果它确实找到了每个文件,那么它就完成了。
我!
我用这个递归函数解决了这个问题
execute_check(){
SEC_TO_WAIT=300
NUM_SEC=
if [ "NUM_SEC${NUM_SEC}" = "NUM_SEC" ]; then
echo "Not waiting"
else
echo "Wait "${NUM_SEC}" seconds"
sleep ${NUM_SEC}
fi
echo "===========================================" 2>&1 | tee -a ${LOG_FILE}
echo "= TRY QUERY ON DB =" 2>&1 | tee -a ${LOG_FILE}
echo "===========================================" 2>&1 | tee -a ${LOG_FILE}
sqlplus -S ${USERSQL}/${PWDSQL}@${DB} > ${SPOOL_FAKE} << EOF
WHENEVER SQLERROR EXIT 1
SET SERVEROUTPUT ON SIZE 1000000;
SET ARRAYSIZE 1;
SET LINESIZE 4000;
SET HEADING OFF;
SET TRIMSPOOL ON;
SET TIMING OFF;
spool ${FLUSSO_ELAB};
select path, file_name
from table
where date=trunc(sysdate);
spool off;
EOF
RESULT=$?
echo "Query result" $RESULT
if [ ${RESULT} = 0 ]; then
echo "===========================================" 2>&1 | tee -a ${LOG_FILE}
echo "= CHECKING FILES EXISTENCE =" 2>&1 | tee -a ${LOG_FILE}
echo "===========================================" 2>&1 | tee -a ${LOG_FILE}
else
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 2>&1 | tee -a ${LOG_FILE}
echo "! Error: !" 2>&1 | tee -a ${LOG_FILE}
echo "! error in the query to DB !" 2>&1 | tee -a ${LOG_FILE}
echo "! Exit code: ${RESULT} !" 2>&1 | tee -a ${LOG_FILE}
echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" 2>&1 | tee -a ${LOG_FILE}
exit 1
fi
# remove first and last row before the loop since they are empty
sed 1d ${SPOOL_FAKE} > ${FLUSSO_ELAB}
rm ${SPOOL_FAKE}
mv ${FLUSSO_ELAB} ${SPOOL_FAKE}
sed '$d' ${SPOOL_FAKE} > ${FLUSSO_ELAB}
rm ${SPOOL_FAKE}
mv ${FLUSSO_ELAB} ${SPOOL_FAKE}
#trasform whitespace in /
sed -i 's/ /\//g' ${SPOOL_FAKE}
while [ `wc -l ${SPOOL_FAKE} | awk -F " " '{print }'` -ge 1 ]
do
echo "-----------------------------------------------------------------------------------------------"
FILE=`head -1 ${SPOOL_FAKE}`
FILE=${FILES_DIR}/${FILE}
echo "Search for file: "${FILE} 2>&1 | tee -a ${LOG_FILE}
if [ -f ${FILE} ]; then
echo "File: "${FILE}" exists." 2>&1 | tee -a ${LOG_FILE}
else
echo "File: "${FILE}" does NOT exist." 2>&1 | tee -a ${LOG_FILE}
execute_check ${SEC_TO_WAIT}
fi;
# ----------------------------------------------------------------------------------------------
# DELETE FIRST ROW TO MOVE FORWARD THE LOOP
# ----------------------------------------------------------------------------------------------
sed 1d ${SPOOL_FAKE} > ${FLUSSO_ELAB}
rm ${SPOOL_FAKE}
mv ${FLUSSO_ELAB} ${SPOOL_FAKE}
done
}
在另一个 .sh 中调用,您在其中初始化了变量
FILES_DIR=${ROOT}/files
SPOOL_FAKE=${ROOT}/log/SpoolFake.txt
FLUSSO_ELAB=${ROOT}/log/FlussoElab.txt
execute_check 0