Db2 LUW - Listing all Local databases in DB2 database server

We come across situations where we will have to get the list of all the databases (specifically Databases local to the Instance) in the Database server.

Below snippet will come handy to achieve this purpose.
This snippet has got 2 parts

  1. grepp --> This is a function written in Perl. More information about this can be found here.
  2. Combination of OS and db2 commands




grepp() { [ $# -eq 1 ] && perl -00ne "print if /$1/i" || perl -00ne "print if /$1/i" < "$2";}
find / -name "db2profile" 2>/dev/null  | grep -i sqllib | while read db2_profile_File; 
do   
  . $db2_profile_File;   
  instanceName=`db2 get instance | grep -v ^$ | awk '{print $7}'`
  instancePortName=`db2 get dbm cfg | grep -w SVCENAME | awk -F"=" '{print $2}'`
  instancePort=`grep -w $instancePortName /etc/services | awk '{print $2}' | cut -d"/" -f1`
  echo "Name of Current instance is --> $instanceName"; 
  echo "Port of Current instance is --> $instancePort"; 
  echo "Local Database(s) under instance $instanceName is below"
  db2 list db directory | grepp Indirect | grep 'Database name' | sort | uniq | awk '{print $4}' | cat -n
  db2 +o terminate
  echo -e "\n\n"
done


Sample output will look like below.



No comments:

Post a Comment