mysql query in shellscript

Hi,

I want to access mysql query from database , for that i have tried the below code

#! /bin/bash
TABLE_NAME=database1
USER_NAME=root
IP_ADDR=111.20.9.256
somevar=`echo "select altid from alert where altid='2724'"| mysql -h $IP_ADDR -u $USER_NAME  $TABLE_NAME `
echo $somevar

the above script gives the output

altid 6

i want the output in somevar is only 6

not altid 6 . how is that ??

Note : please try to change the code only in

somevar=`echo "select altid from alert where altid='2724'"| mysql -h $IP_ADDR -u $USER_NAME  $TABLE_NAME `

i dont want to increase the line of script becoz same concept i want to apply to many querys .

Please help me .

Thanks

somevar=$( mysql -h "$IP_ADDR" -u "$USER_NAME"  "$TABLE_NAME" -BNe"select altid from alert where altid='2724'" )    
1 Like