Pages

Thursday, January 9, 2020

Code Example: Simple bash with sql inside

#!/bin/bash

#---------------------------------
#functions
#---------------------------------
report_error(){
  db_user=$1
  echo ==========================================
  echo Error!!!
  echo User $db_user Connection Check........Failed  
  echo Connection Error for User $db_user
  echo ERROR: ORA-01017: invalid username/password. logon denied
  echo Exiting script!
  echo ==========================================
  exit 1
}

report_success(){
  db_user=$1
  echo User $db_user Connection Check........Passed 
}


#---------------------------------
#main
#---------------------------------

echo ==========================================
echo Mark End of Upgrade Start
echo ==========================================


export OUTPUT_FILE=./chk_connections.log

VIPUSER=`grep vipuser ../set_vipuser.sql |awk -F= '{print $2}'`
VIPPASS=`grep vippass ../set_vipuser.sql |awk -F= '{print $2}'`
CONNECTSTR=`grep "connectstr=" ../set_vipuser.sql |awk -F= '{print $2}'`

rm -f $OUTPUT_FILE
touch $OUTPUT_FILE

echo Check Connection for:  ${VIPUSER}/${VIPPASS}@${CONNECTSTR} >> $OUTPUT_FILE
sqlplus -s ${VIPUSER}/${VIPPASS}@${CONNECTSTR} >> $OUTPUT_FILE << EOD
set serveroutput on
set heading off linesize 130 pagesize 1000  feedback off
SELECT 'Connection is OK for '||USER FROM DUAL;
EOD


#----------------------------
# Analyze Connection Results
#----------------------------
connection_ok=`grep "Connection is OK for" $OUTPUT_FILE | grep -iw ${VIPUSER} | wc -l`
if [[ $connection_ok != 2 ]];then
  report_error ${VIPUSER}
else
  report_success ${VIPUSER}
fi

echo "Mark GG_REF_TABLES_LIST for Update Completion"
sqlplus -s ${VIPUSER}/${VIPPASS}@${CONNECTSTR} >> $OUTPUT_FILE << EOD
set serveroutput on
set heading off linesize 130 pagesize 1000  feedback off
UPDATE GG_REF_TABLES_LIST SET replicated_data_ind='Y' WHERE replicated_data_ind IS NULL OR replicated_data_ind='N';
commit;
EOD


echo ==========================================
echo Mark End of Upgrade Finish
echo ==========================================

exit 0 

No comments:

Post a Comment