Shell Script xmpSetInstance.sh – set DB2 environment
Activate requested DB2 environment:
Search for registered DB2 software version, instances, databases and execute the appropriate DB2 environment. The KSH shell script is quite useful on sites with more than one DB2 instance.
#!/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. # How to determine and check DB2 profile registry for installed # DB2 instances: < V9.1: /tmp/profiles.reg # = V9.1: /opt/IBM/db2/V9.1/profiles.reg # = V9.7: /var/[opt/]db2/global.reg # Since Version 9.1 DB2 provides the db2ls command which lists all # installed products and their installation paths. Using the most # current version of the db2greg command will list all installed # instances. # ------------------------------------------------------------------------------ # Autor G.Ruban /22.05.02 - erste Version V1.0 # Changes G.Ruban /09.07.02 - V1.2: equal operand differs on platforms # G.Ruban /28.08.02 - V1.2.1: equal operand differs on platforms # G.Ruban /22.10.02 - V1.2.2: unset variables # G.Ruban /05.12.06 - V2.0: Some amendments necessary for DB2 V9.1 # G.Ruban /14.05.07 - V2.1: use /usr/local/bin if db2ls not in path # G.Ruban /13.07.11 - V3.0: Upgrade to DB2 V9.7, profiles.reg not # longer supported # G.Ruban /09.09.11 - V3.1: Problem new with AIX 6.1 solved ################################################################################ #------------------------------------------------------------------------------- # Init #------------------------------------------------------------------------------- PROG="xmpSetInstance.sh" VER="3.1" 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" $StringEQ "?" ] ; 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 db2ls command to inquire installed software #------------------------------------------------------------------------------- db2lspath=`which db2ls` rc=$? if [ $rc -ne 0 ] ; then echo ">>> $PROG: DB2 Utility db2ls not found in current path." db2lspath="/usr/local/bin/db2ls" echo ">>> Execution path set to $db2lspath." fi #------------------------------------------------------------------------------- # Inquire latest installation path of DB2 (provides 1 item) #------------------------------------------------------------------------------- db2sw=`$db2lspath | grep db2 | awk '{print $1}' | tail -1` rc=$? if [ $rc -ne 0 ] ; then echo "Neither DB2 V9.7 nor V9.1 found, continue searching older versions ..." echo "abends with RC=16" exit 16 fi if [ ! "$db2sw" ]; then echo "$0: DB2 command db2ls found but no installed DB2 products found!" echo "abends with RC=16" exit 16 fi #------------------------------------------------------------------------------- # For archive reasons only: Determination of profiles.reg and global.reg # < V9.1: /tmp/profiles.reg # = V9.1: /opt/IBM/db2/V9.1/profiles.reg # = V9.7: /var/[opt/]db2/global.reg # regfile=`find /var/db2/ -name 'global.reg' -type f -print;find /var/opt/db2/ -name 'global.reg' -type f -print;find /var/db2/ -name 'profiles.reg' -type f -print;find $db2ip -name 'profiles.reg' -type f -print` #------------------------------------------------------------------------------- #------------------------------------------------------------------------------- # Inquire all instances from DB2 registry, list instance name and path only #------------------------------------------------------------------------------- db2greg=$db2sw"/bin/db2greg -dump" #echo ">>> $db2greg" ilist=`$db2greg | awk -F, '/I,DB2/ {print $4":"$5}'` #echo ">>> $ilist" #------------------------------------------------------------------------------- # 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" echo "List of instances and databases available at this server ..." #------------------------------------------------------------------------------- # 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 instances #------------------------------------------------------------------------------- maxrc=0 #echo ">>>$ilist<<<" for i in $ilist do #--------------------------------------------------------------------------- # split instance name and instance path #--------------------------------------------------------------------------- instname=${i%:*} instpath=${i#*:} #--------------------------------------------------------------------------- # Check existance of db2profile script (for execution in next step) #--------------------------------------------------------------------------- if [ -x "$instpath/db2profile" ]; then echo "- Instance / Home Directory : $instname $instpath" 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 ". $instpath/db2profile; db2 list db directory|egrep '(alias|type)'|cut -f2 -d'='; 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]=$instname DBP[$dbi]=$instpath;; esac 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]})" RC=0 fi #----------------------------------------------------------------------- # Activate instance environment if instance name found #----------------------------------------------------------------------- if [ "$search" != "" -a "$search" $StringEQ "${DBI[$t]}" ]; then #echo ">>>${DBP[$t]}/db2profile" . ${DBP[$t]}/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 #echo ">>>${DBP[$t]}/db2profile" . ${DBP[$t]}/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 #------------------------------------------------------------------------------- # Remove variables #------------------------------------------------------------------------------- unset db2sw db2greg unset ilist list unset db dbi dbl unset r i ix p pi t search maxrc rc unset instname instpath unset regfile unset DBI[*] DBN[*] DBP[*] DBT[*] echo "$PROG finished (RC=$RC)." return $RC
Comments
Comments are closed.