Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Echo displaying the SQL command as well as the result

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It should display Interest_3_Map

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading