Listing 1 makehpl.ss
# Listing 1:
#!/bin/ksh
# makehpl.ss - script to build an informix HPL job in the
# onpload database
PROGRAM_NAME=$0
SUCCESS=0
INVALID_OPTION=1
ERROR=2
INVALID_USER=3
DBNAME="onpload" # onpload database name
# variables that might change
INTERFACE_CMD="dbaccess"
DBCOMMAND="$INTERFACE_CMD -e $DBNAME"
DIRPATH="\/tmp\/" # directory to the file
PROGRAM_LOCATION=/home/eds/articles/hpl # directory where this script exists.
INFORMIXDIR=/usr/informix
PATH=/sbin:/usr/sbin:/usr/bin:/usr/openwin/bin:$INFORMIXDIR/bin:/usr/local/bin
cd $PROGRAM_LOCATION
# you must be the informix user
[[ $(id | cut -d'(' -f2 | cut -d')' -f1) != "informix" ]] && \
{ echo "You must be user informix"; exit $INVALID_USER; }
function usage {
echo "Usage: $PROGRAM_NAME -d database -t tablename -w where_clause \
-n numdevices"
echo
exit $INVALID_OPTION
} # end usage
function is_integer {
[[ $1 = ?([+-])+([0-9]) ]]
}
# quit if no arguments
[[ "$#" -eq 0 ]] && usage
while getopts d:t:w:n: OPTION
do
case "$OPTION"
in
d) database="$OPTARG"
;;
t) tablename="$OPTARG"
;;
w) where_clause="$OPTARG"
;;
n) num_unload_files="$OPTARG"
;;
*) usage
;;
esac
done
shift $(($OPTIND-1))
# default num_unload_files to 1 if it isn't set on the command line
is_integer $num_unload_files || num_unload_files=1
[[ -z $database ]] && { echo "database undefined"; exit $INVALID_OPTION ; }
[[ -z $tablename ]] && { echo "tablename undefined"; exit $INVALID_OPTION ; }
# build the select statment
select_stmt="select * from $tablename $where_clause"
# blow away the temp files if any
rm -f ./devicetemp.unl ./querytemp.unl ./sessiontemp.unl
# build the query table record
# careful: echoing the $select_stmt expands * which includes all objects
# in the current directory
sed s/TABLE_NAME/$tablename/g ./querytemplate.txt|sed s/DATABASE/$database/g| \
sed s/SELECT_STATEMENT/"$select_stmt"/g > ./querytemp.unl
# build the device table record(s)
device_num=0
while [ $device_num -ne $num_unload_files ]
do
((device_num=$device_num+1))
sed s/TABLE_NAME/$tablename/g ./devicetemplate.txt| \
sed s/DEVICE_NUMROWS/$device_num/g|sed \
s/DIRPATH/$DIRPATH/g >> ./devicetemp.unl
done
# build the session table records
sed s/TABLE_NAME/$tablename/g ./sessiontemplate.txt| \
sed s/DATABASE/$database/g > ./sessiontemp.unl
# delete the job from the onpload database if it happens to exist
$DBCOMMAND << MSG
DELETE from session WHERE name = "$tablename";
DELETE from device WHERE name = "$tablename";
DELETE from query WHERE name = "$tablename";
MSG
[[ "$?" -ne 0 ]] && { echo "delete failure"; exit $ERROR ; }
# create the session table records
$DBCOMMAND << MSG
load from ./sessiontemp.unl insert into session
MSG
[[ "$?" -ne 0 ]] && { echo "session load failure"; exit $ERROR ; }
# create the device table record(s)
$DBCOMMAND << MSG
load from ./devicetemp.unl insert into device
MSG
[[ "$?" -ne 0 ]] && { echo "device load failure"; exit $ERROR ; }
# create the query table record
$DBCOMMAND << MSG
load from ./querytemp.unl insert into query
MSG
[[ "$?" -ne 0 ]] && { echo "query load failure"; exit $ERROR ; }
exit $SUCCESS |