Created
August 20, 2013 10:01
-
-
Save angoca/6279599 to your computer and use it in GitHub Desktop.
Change between multiple DB2 instances in the same server. It has two modes, interactive or with parameter.
The script is written in bash for DB2 9.7 but it can be used in any previous or more recent version.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Changes the environment to the given DB2 instance. | |
# | |
# You need to 'source' this command in order to change the environement. | |
# To ease the process, you can create a function per instance in the .profile. | |
# | |
# function i01 { . ${CHINS} db2inst1 ; } | |
# | |
# To configure, you just need to change the DB2 path. | |
# | |
# 20130601 A. Gomez (AngocA) - Creation script. | |
# 20130617 A. Gomez (AngocA) - Modification after refactoring to get close to oraenv. | |
# DB2 path | |
DB2_DIR=/opt/ibm/db2/9.7/ | |
#set -x | |
changeInstance () { | |
# Instance name (lower case) | |
INSTANCE=$1 | |
PROFILE_DIR="~/${INSTANCE}/sqllib/db2profile" | |
RETURN=0 | |
if [[ -f ${PROFILE_DIR} ]] ; then | |
. ${PROFILE_DIR} | |
else | |
echo "DB2 profile does not exist for the given instance" | |
RETURN=-1 | |
fi | |
# Disconnect any existing connection. | |
CONN=$(db2 connect | awk '/Local database alias/ {print $5}') | |
if [[ ! -z ${CONN} ]] ; then | |
echo "Disconnected from ${CONN}" | |
fi | |
# Terminates the previous db2bp (DB2 backend process) | |
db2 terminate > /dev/null | |
echo "Current instance ${DB2INSTANCE}" | |
return ${RETURN} | |
} | |
# Instance name (lower case) | |
INSTANCE=$1 | |
if [[ -f ${DB2_DIR}/bin/db2greg ]] ; then | |
DB2GREG=${DB2_DIR}/bin/db2greg | |
else | |
echo "DB2 binaries (DB2GREG) location unknown." | |
exit -1 | |
fi | |
# Checks if an instance name was given. | |
if [[ -z ${INSTANCE} ]] ; then | |
PS3='Please select an instance: ' | |
INDEX=0 | |
while read LINE ; do | |
CLIENTS[$INDEX]="$LINE" | |
INDEX=$(($INDEX+1)) | |
done < <(${DB2GREG} -dump | awk -F, '/^I,/ {print $4}') | |
QUIT=Quit | |
CLIENTS[$INDEX]=${QUIT} | |
select OPT in "${CLIENTS[@]}" | |
do | |
if [[ ${OPT} != ${QUIT} ]] ; then | |
INST_VALID=$(${DB2GREG} -dump | grep I, | grep ,${OPT},\/) | |
# Checks if the instance exists in the server | |
if [[ -z ${INST_VALID} ]] ; then | |
echo "Invalid instance: ${INSTANCE}" | |
else | |
changeInstance ${OPT} | |
if [[ $? -eq 0 ]] ; then | |
break | |
fi | |
fi | |
else | |
break | |
fi | |
done | |
else | |
INST_VALID=$(${DB2GREG} -dump | grep I, | grep ,${INSTANCE},) | |
# Checks if the instance exists in the server | |
if [[ -z ${INST_VALID} ]] ; then | |
echo "Invalid instance: ${INSTANCE}" | |
else | |
changeInstance ${INSTANCE} | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the evolution of chins. This is more dynamic.