Skip to Main Content

Infrastructure Software

Announcement

Testing banner

Adding couple of conditions to existing shell script

User_QDHXFOct 3 2022

Hello,
I totally newbie towards Shell scripting. We have an existing script as below:
#!/bin/bash
# Script that will kick off reading from the Oracle view
# to get the list of providers into a json formatted file for use in the provider module.
# The companion file is sql_query.sql to pull the correct fields from
# Oracle and then the data is massaged below to remove any unwanted characters

source ~/.bashrc
# Needed by SQL*Plus on DEV
# Old DEV - ORACLE_HOME=/app/app/oracle/product/12.2.0/client_1 ; export ORACLE_HOME
ORACLE_HOME=/app/oracle/app/oracle/product/19.0.0/client_1 ; export ORACLE_HOME
SQL_CMD=/app/oracle/app/oracle/product/19.0.0/client_1/bin/sqlplus

# Specify the location of the final file
#OUTFILE=/home/ec2-user/provider_list.json
#OUTDIR=/home/ec2-user
OUTDIR=/app/tableauTomcat/webapps/docs
OUTFILE=/app/tableauTomcat/webapps/docs/provider_list.json
PROVFILE=provider_list.json

# Development Credentials
CREDS=tabdata/tabdata@dev.clo0fiz.us-east-1.rds.amazonaws.com/dev

DATE=`date +%Y%m%d_%H_%M`
BASE_DIR=${OUTDIR}
if [ -f $BASE_DIR/$PROVFILE ]; then
cp $BASE_DIR/$PROVFILE $BASE_DIR/$PROVFILE.$DATE
/usr/bin/gzip $BASE_DIR/$PROVFILE.$DATE
fi

# Find and remove any files older than 3 days
find $BASE_DIR -name "${PROVFILE}.*" -a -mtime +3 -exec rm {} \;
echo ${OUTFILE}
echo ${ORACLE_HOME}
echo ${SQL_CMD}
echo ${PATH}

echo -n "Starting p_provider_timeline procedure: "
date '+%Y%m%d-%H%M%S'

${SQL_CMD} /nolog << EOF0
CONNECT ${CREDS}
exec p_provider_timeline;

I have been asked to do the following on this script:
Before anything gets executed from the script, check Oracle DB Connection.
If DB connection is successful, proceed normally.
If DB connection is unsuccessful, then send an email to admin team and quite from the script, without attempting to execute anything.
Can someone please help me how to achieve these? Any help is greatly appreciated.

Thanks.

This post has been answered by User_IBB8Q on Oct 3 2022
Jump to Answer
Comments
Post Details
Added on Oct 3 2022
5 comments
66 views