oracledba.help
Scripts

BASH Script template2

<- Scripts

Designed for more complex scripts, this template is concise yet allows you to have an unlimited number of included lib functions as needed.

MyScript.sh

#!/bin/bash
# Purpose:      MyScript.sh (Brief_Purpose_Statement_Here)
# Version:      2018.10.01
# Dependencies: inc_system.sh

######################################
# Preamble: 2018.10.01               #
######################################
set -a; DIR_SCRIPTS="/u01/app/scripts"; source $DIR_SCRIPTS/inc_system.sh
sFullName=$(basename "$0"); me=${sFullName%.*}
sSLog="$DIR_LOGS/$me.sess.log"; sHLog="$DIR_LOGS/$me.hist.log"; > $sSLog
if [[ $MAINT_WINDOW -eq 1 ]]; then printf "Maintenance Window Detected - Exiting\n"; exit; fi

# User Vars
usrEmailList="scott@mycompany.com"

######################################
# Init Script Actions\Functions\Vars #
######################################
# Vars

# Functions


######################################
# Start                              #
######################################
clear; log "$sSLog" "$sFullName Started"; linesep
SysVars_show

# <<< Core Script Cmds Here >>>

######################################
# End                                #
######################################
log "$sSLog" "$sFullName Ended [Elapse Time: $(elapse)]";
echo $(linesep "=") >> $sHLog; cat $sSLog >> $sHLog;
tail -32768 $sHLog > $sHLog.tmp; mv $sHLog.tmp $sHLog

inc_system.sh

#!/bin/bash
# Purpose: Common include file for scripts.
# Version: 2018.10.01

# Oracle
ORACLE_SID="oradb";
ORACLE_BASE="/u01/app/oracle";
ORACLE_HOME="/u01/app/oracle/product/12.1.0.2/dbhome_1";

# Dirs
DIR_LIB="$DIR_SCRIPTS/lib";
DIR_LOGS="$DIR_SCRIPTS/logs";
DIR_OUT="$DIR_SCRIPTS/out";
DIR_TMP="$DIR_SCRIPTS/tmp";
if [[ ! -d $DIR_LIB ]];  then mkdir $DIR_LIB;  fi
if [[ ! -d $DIR_LOGS ]]; then mkdir $DIR_LOGS; fi
if [[ ! -d $DIR_OUT ]];  then mkdir $DIR_OUT;  fi
if [[ ! -d $DIR_TMP ]];  then mkdir $DIR_TMP;  fi

# Misc
MAINT_WINDOW=0;
START_SEC="$SECONDS";
TERM=xterm

#############
# Functions #
###############################################################################
SysVars_show() {
   printf "DIR_SCRIPTS: $DIR_SCRIPTS\n"
   printf "DIR_LIB:     $DIR_LIB\n"
   printf "DIR_LOGS:    $DIR_LOGS\n"
   printf "DIR_OUT:     $DIR_OUT\n"
   printf "DIR_TMP:     $DIR_TMP\n"
   linesep
}

elapse()  { local nSecs=$(($SECONDS - $START_SEC)); local nMins=1;
            if [[ $nSecs -ge 60 ]]; then nMins=$(($nSecs/60)); fi; echo $nMins; }
linesep() { if [[ -z $1 ]]; then printf '%80s\n' | tr ' ' -;
            else printf '%80s\n' | tr ' ' $1; fi; } 
log()     { printf "`now`: $2\n" | tee -a $1; }
now()     { local sNow=$(date "+%Y-%m-%d %H:%M:%S"); echo $sNow; } 
pause()   { read -p "Press [Enter] to continue."; }

$DIR_SCRIPTS/lib/inc_sqlplus.sh (example library function)

#!/bin/bash
# File:    inc_sqlplus.sh 
# Version: 2018.10.01
# Usage:   sMyValue=$(xsql "SELECT sysdate FROM dual;") 
# Assumes: DIR_TMP & ORACLE_HOME vars init via inc_system.sh

xsql() {
   # Init Vars
   local nRnd=$(shuf -i100-999 -n1)
   local sSQL="$1";
   local sSQLPlusInit="set feedback off linesize 256 pagesize 0 trimspool on verify off";
   local fScript="$DIR_TMP/xsql.$nRnd.tmp";
   local fSpool="$DIR_TMP/xsql.$nRnd.spool";
   if [[ $2 -eq 1 ]]; then
      nKeepSpoolFile=1;
   fi

   cat > $fScript <<-EOF
   $sSQLPlusInit
   spool $fSpool
   $sSQL
   spool off
   exit
EOF

   # Run SQL Script
   CMD="`$ORACLE_HOME/bin/sqlplus / as sysdba @$fScript $fSpool`";
   sValue=`cat $fSpool| awk '{gsub(/^ +| +$/,"")} {print "" $0 ""}'`;

   # End
   if [[ $nKeepSpoolFile -eq 1 ]]; then
      echo $fSpool
   else
      echo $sValue
      rm $fSpool
   fi
   rm $fScript
}