configCheck ()
{
# checks to see whether a db key exists
# version 1.0
#
# required inputs:
# ccKey - key to check for
# ccDbs - databases to check in
#
# returns:
# 0 - key does not exist
# 1 - key exists
# 1001 - function error
#
# instructions for use:
# testDbs=( accounts configuration )
# configCheck "myKey" testDbs
# if [ $? -eq 1 ]; then
# echo "Key exists!"
# fi
local ccKey=$1
local ccDbs=;
local ccReturn=1001
# Setting the shell's Internal Field Separator to null
# from http://www.unix.com/shell-programming-scripting/61370-bash-ksh-passing-array-function.html
local OLD_IFS=$IFS
IFS=''
# Create a string containing "dbs[*]"
local ccReadArray="$2[*]"
# assign loc_array value to $dbs[*]} using indirect variable reference
ccDbs=(${!ccReadArray})
# Resetting IFS to default
IFS=$OLD_IFS
writeLog "Checking database for key $ccKey."
for element in $(seq 0 $((${#ccDbs[@]} - 1)));
do # ${#ccDbs[@]}
#+ gives number of elements in the array.
#
# Question:
# Why is seq 0 necessary?
# Try changing it to seq 1.
writeLog "Checking database ${ccDbs[$element]}."
runCommand "db ${ccDbs[$element]} show $ccKey" "quiet"
if [ $? -eq 0 ]; then
# previous data exists
writeLog "Configuration key detected in ${ccDbs[$element]} database!";
ccReturn=1;
elif [ $? -eq 1 -a $ccReturn -ne 1 ]; then
ccReturn=0;
fi
done
return $ccReturn;
} # configCheck
|