Getting variable from a query

Hi,

I need to get a variable HMAX_TBL_ID and then use it in second select query. The first select statement returns few leading white spaces for ex : 12345
so when I run the script HMAX_TBL_ID does not return the "12345" , it returns blank.
Please let me know what I am missing.

HMAX_TBL_ID=`psql -U ${PG_USER} -h ${PG_HOST} -d ${PG_DB} -p ${PG_PASS} -t -c "select stp_ctr_fetch_module_tbl_id('ATTRIB', 'GET')"` | sed -e 's/^[ \t]*//'

        echo -e "This is max table id in ${HMAX_TBL_ID} "

        if [ $? -ne 0 ]
        then
                echo -e " Error while getting the maximun table id processed"
        fi


  psql -U ${PG_USER} -h ${PG_HOST} -d ${PG_DB} -p ${PG_PASS} -t -c "select distinct tbl_id from tbl_ctr_module_tbl where tbl_id between ${LAST_PROCESSED_TABLEID} and ${HMAX_TBL_ID}" | sed '/^$/d' > ${TBL_ID_FILE}

Stating what database and shell you are using would help, but since that info is not here...

Here's a couple of ways. First, try putting put a TRIM( ) function call (or your system's equivalent) around

stp_ctr_fetch_module_tbl_id('ATTRIB', 'GET')

Or in the script try one of these:

$ cat x
a="  1234"
print "Before: [$a]"
typeset -i a   # make it an integer.
print "After: [$a]"

b="  1234"
print "Before: [$b]"
b=$(print $b)
print "After: [$b]"


$ ./x
Before: [  1234]
After: [1234]
Before: [  1234]
After: [1234]
$
1 Like