Skip to content
Advertisement

sqlplus query + loop on linux

I have pratically 0 experience on unix and few on mysql and I am finding hard where to start to complete this task, I have to write a .sh file to launch once a day.

I have a table on OracleDb where I have 2 columns I need: path and file_name.

SELECT path, file_name FROM table WHERE date=trunc(sysdate);

Once I get the results, I should loop through the file system to check if the file in its path exists, if it does not, the script should sleep for few minutes and repeat from the query.

if it does find every file, then it’s completed.

Advertisement

Answer

me!

I solved this with this recursive function

execute_check(){
SEC_TO_WAIT=300
NUM_SEC=$1
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 $1}'` -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
}

Called in another .sh, where you initialized the variables

FILES_DIR=${ROOT}/files
SPOOL_FAKE=${ROOT}/log/SpoolFake.txt
FLUSSO_ELAB=${ROOT}/log/FlussoElab.txt
execute_check 0
5 People found this is helpful
Advertisement