0

I'm trying to use a bash script to sanitize a database and I need to use the largest ID Number 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 that line in my script is

max(idnumber) 3

How can I parse the output of the mysql command so that MAXID is just 3?

2 Answers2

1

Use the --skip-column-names (or -N for short) option to omit column name headings in the output:

MAXID=$(mysql -u root -proot -N elis27 -e "select max(idnumber) from mdl_user;")
Barmar
  • 741,623
  • 53
  • 500
  • 612
1

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

a="max(idnumber) 3"
b=`echo $a | awk '{print $2}'`;echo $b
shamik
  • 653
  • 1
  • 7
  • 13