DB2 ist ein Produkt der
IBM Corporation. Bitte Copyright- und Trademark-Hinweise beachten!
#!/bin/ksh
################################################################################
# xmpSetInstance.sh - set DB2 environment
# ------------------------------------------------------------------------------
# Parameter: inst|db - instance or database name
# LIST- list databases
#
# System : AIX
# SunOS (Solaris)
#
# Comment : On servers with more than one DB2 instance, user probably needs to
# switch between instances. The original IBM korn shell script
# ". db2dprofile" does not support switching between instance, but
# concatenates instance settings.
# ------------------------------------------------------------------------------
# Autor G.Ruban /22.05.02 - erste Version V1.0
# Changes G.Ruban /09.07.02 - V1.2: equal operand differs on platforms
################################################################################
#-------------------------------------------------------------------------------
# Init
#-------------------------------------------------------------------------------
PROG="xmpSetInstance.sh"
VER="1.2"
ldbcmd="db2 list db directory|egrep '(alias|type)'|cut -f2 -d"=""
typeset -i dbi
typeset -i t
dbi=0
#-------------------------------------------------------------------------------
# Plaform-specific
#-------------------------------------------------------------------------------
case `uname` in
AIX)
StringEQ="="
;;
SunOS)
StringEQ="=="
;;
*)
echo "$PROG: Unknown plaform, is not AIX or SunOS."
exit 8
;;
esac
#-------------------------------------------------------------------------------
# Check arguments
#-------------------------------------------------------------------------------
if [ $# -lt 1 -o $1 = ? ] ; then
echo "Requested syntax: $PROG [inst|db] [LIST]"
echo "... where inst = name of local instance"
echo " db = name of local database"
echo " LIST = also lists databases"
echo "abends with RC=4"
exit 4
fi
typeset -u search=$1
typeset -u list=$2
if [ $# -eq 1 -a "$search" $StringEQ "LIST" ]; then
search=""
typeset -u list=$1
fi
#-------------------------------------------------------------------------------
# Determine and check DB2 profile registry
#-------------------------------------------------------------------------------
regfile=`find /var/db2/ -name 'profiles.reg' -type f -print`
if [ ! "$regfile" ]; then
echo "$0: No DB2 instances installed or no profiles.reg found!"
echo "abends with RC=16"
exit 16
fi
#-------------------------------------------------------------------------------
# Provide information
#-------------------------------------------------------------------------------
echo $PROG " - V$VER"
echo "======================================================="
echo "Current User ................: "`id`
echo "Current Date/Time ...........: "`date +%Y-%m-%d-%H.%M.%S`
echo "System ......................: "`uname -n`
echo "Required Instance or Database: $search"
#-------------------------------------------------------------------------------
# Check for repeated invocation
#-------------------------------------------------------------------------------
if [ "$lP_BANN" $StringEQ "$PROG" ]; then
echo "-------------------------------------------------------"
echo "Warning, $PROG invoked repeatedly!"
echo "DB2 environment may not be set correctly!"
echo "Use env command to check PATH, LIBPATH etc."
echo "-------------------------------------------------------"
fi
#-------------------------------------------------------------------------------
# Process all occurences of profiles.reg (for each each version)
#-------------------------------------------------------------------------------
maxrc=0
for r in $regfile
do
#-----------------------------------------------------------------------
# Check registry for read authorization
#-----------------------------------------------------------------------
if [ ! -r $r ] ; then
echo "$0: cannot read $r!"
echo "abends mit RC=16"
exit 16
else
#---------------------------------------------------------------
# Print profiles.reg, contains instances
#---------------------------------------------------------------
echo "DB2 Instance Registry File ..: $r"
ilist=`cat $r`
#echo ">>>$ilist<<<"
for i in $ilist
do
#-------------------------------------------------------
# Scan /etc/passwd for instance home directory
#-------------------------------------------------------
#echo ">>>$i<<<"
pi=`cat /etc/passwd | grep $i | awk -F: '{print $6}'`
#echo ">>>$?<<<"
for p in $pi
do
#-----------------------------------------------
# Check existence of db2profile script
#-----------------------------------------------
#echo ">>>$p<<<"
if [ -x "$p/sqllib/db2profile" ]; then
#---------------------------------------
# Store instance
#---------------------------------------
typeset -u ix=$i
echo "- Instance / Home Directory : $ix $p"
dbi=$dbi+1
DBT[$dbi]="see below"
DBN[$dbi]="list of databases"
typeset -u DBI[$dbi]=$i
DBP[$dbi]=$p
#---------------------------------------
# List database directory at instance
#---------------------------------------
dbl=`ksh -c ". $p/sqllib/db2profile; $ldbcmd; exit"`
rc=$?
#---------------------------------------
# Store database names and types
#---------------------------------------
for db in $dbl
do
case $db in
Indirect) typeset -u DBT[$dbi]=$db;;
Remote)typeset -u DBT[$dbi]=$db;;
*)dbi=$dbi+1
typeset -u DBN[$dbi]=$db
typeset -u DBI[$dbi]=$i
DBP[$dbi]=$p;;
esac
done
fi
done
done
fi
done
#-------------------------------------------------------------------------------
# Activate instance environment if instance name or database found
#-------------------------------------------------------------------------------
if [ "$list" $StringEQ "LIST" ]; then
echo "-------------------------------------------------------"
echo "List of instances, databases :"
fi
RC=4
t=1
while [ $t -le $dbi ]
do
#-----------------------------------------------------------------------
# List database directory if requested
#-----------------------------------------------------------------------
if [ "$list" $StringEQ "LIST" ]; then
echo "- Instance / Database (Type) : ${DBI[$t]} / ${DBN[$t]} (${DBT[$t]})"
fi
#-----------------------------------------------------------------------
# Activate instance environment if instance name found
#-----------------------------------------------------------------------
if [ "$search" != "" -a "$search" $StringEQ "${DBI[$t]}" ]; then
. ${DBP[$t]}/sqllib/db2profile
RC=$?
echo " => DB2 environment for instance ${DBI[$t]} set with RC=$RC."
search=""
lP_BANN=$PROG
export lP_BANN
fi
#-----------------------------------------------------------------------
# Activate instance environment if local database name found
#-----------------------------------------------------------------------
if [ "$search" != "" -a "$search" $StringEQ "${DBN[$t]}" -a "${DBT[$t]}" $StringEQ "INDIRECT" ]; then
. ${DBP[$t]}/sqllib/db2profile
RC=$?
echo " => DB2 environment for instance ${DBI[$t]} / ${DBN[$t]} set with RC=$RC."
search=""
lP_BANN=$PROG;
export lP_BANN
fi
t=$t+1
done
echo "$PROG finished (RC=$RC)."
return $RC
© Gernot Ruban