Parse mysql command output into variable in bash script

I am trying to use a bash script to disinfect a database, and I need to use the largest ID from the users table, so I have this line in my script

MAXID=$(mysql -u root -proot elis27 -e "select max(idnumber) from mdl_user;") echo $MAXID 

And the output of this line in my script is equal

 max(idnumber) 3 

How can I parse mysql command output so that maxid is only 3?

0
bash mysql
Aug 28 '14 at 1:07
source share
2 answers

Use the --skip-column-names parameter (or -N for short) to omit the column headers in the output:

 MAXID=$(mysql -u root -proot -N elis27 -e "select max(idnumber) from mdl_user;") 
+1
Aug 28 '14 at 1:24
source share

I will let you put the awk statement in the maxid declaration, here is a simple logic to get 3 -

 a="max(idnumber) 3" b=`echo $a | awk '{print $2}'`;echo $b 
0
Aug 28 '14 at 1:26
source share



All Articles