#!/bin/bash
# Establishing database connection
db2 "connect to itgdb"
# Gathering the tablespaces other than temporary and placing them in a file
db2 list tablespaces show detail | grep -i -A2 name | tac | sed '/Temporary/I,+2 d' | tac | grep -i name | awk '{print $3}' > /tmp/TablespaceList.out
# Using for loop to get the tablespace name one by one
for i in `cat /tmp/TablespaceList.out`
do
# Gathering free pages count for each tablespace
db2 list tablespaces show detail | grep -i -A9 $i | grep -i 'free pages' | awk '{print $4}' > /tmp/FreePages.out
FreePages=`cat /tmp/FreePages.out`
# Comparing whether the free pages are more than 1000 or not
if [ $FreePages -gt 1000 ]
then
# If the free pages are more than 1000 then reducing them
db2 "alter tablespace $i reduce max"
fi
done
# Establishing database connection
db2 "connect to itgdb"
# Gathering the tablespaces other than temporary and placing them in a file
db2 list tablespaces show detail | grep -i -A2 name | tac | sed '/Temporary/I,+2 d' | tac | grep -i name | awk '{print $3}' > /tmp/TablespaceList.out
# Using for loop to get the tablespace name one by one
for i in `cat /tmp/TablespaceList.out`
do
# Gathering free pages count for each tablespace
db2 list tablespaces show detail | grep -i -A9 $i | grep -i 'free pages' | awk '{print $4}' > /tmp/FreePages.out
FreePages=`cat /tmp/FreePages.out`
# Comparing whether the free pages are more than 1000 or not
if [ $FreePages -gt 1000 ]
then
# If the free pages are more than 1000 then reducing them
db2 "alter tablespace $i reduce max"
fi
done