I am trying to write a script which connects to a database then run a query that returns count of rows in a table. sometimes database or table have problems and instead of an integer they return an error. for example:
A database connection does not exist
i want to determine if output is the integer i want or its an error. i searched through various posts but none of them worked in my script. what should i do?
DB2PATH=/bin/db2
$DB2PATH connect to $1 >> /dev/null
count=$($DB2PATH -x " select count(*) from TEST122 ")
if ! [[ "$count" =~ ^[0-9]+$ ]]
then
echo "string"
else
echo $count
fi
i changed the if condition in many different ways. i used -eq
and -ne
. it keep returning string
. and the value of count
is 11.
update:
printf '%s' "$count" | hexdump -c
output:
0000000 1 1
000000b
count
is actually11
, theif
works fine for me. Maybe the command output has spaces in it, or quotes, or some other weird characters.printf '%s' "$count" | hexdump -c
and paste above?