I have a variable which is finding the latest id from a db table, i am then looking to append the result of this query to the middle of another variable, this is because each time the script is ran it needs to create a new table incrementing from the previous id
TABLE_NUMBER=$(mysql -u $2 $3 -h $4 -e "SELECT Max(Id) 1 FROM TableofInterest;" db_name)
echo "table number is"
echo $TABLE_NUMBER
LATESTID="Interest_${TABLE_NUMBER}_Map"
echo $LATESTID
The above displays the result as:
Thu 4 Aug 2022 15:08:57 UTC
running the script
mysql: [Warning] Using a password on the command line interface can be insecure.
table number is
Max(Id) 1 3
Interest_Max(Id) 1 3_Map
I was wanting it to display just the number, not the Max(id) 1
It should display Interest_3_Map
CodePudding user response:
Perhaps:
TABLE_NUMBER=$(mysql -u $2 $3 -h $4 -e "SELECT Max(Id) 1 FROM TableofInterest;" db_name | sed -n 2p)
Most flavors of SQL print the output of commands as a table with a header labeling each column. If you don't want that header then you can filter the output to remove it. In this case sed -n 2p
prints only the second line of output which should be the result you care about.