Solved

Run mysql select query from bash file

Posted on 2008-10-31
4
3,655 Views
Last Modified: 2012-05-05
I need to run a select query in bash script and pass the result to a variable

----------

#!/bin/bash

mysql > SELECT Updated FROM LastUpdate ORDER BY Updated DESC LIMIT 0, 1;

//Now I need to pass the result to a var to use here

UpdatedResult = `query result?`

mysqldump -h hostname --user=mylogin --password=mypass --no-create-info --opt --where="LastUpdate>${UpdatedResult}" MyDb MyTable |sed -e "s|INSERT INTO|REPLACE INTO|" > MyDump_replace.sql

-----

So Basically I need to be able to query one table retrieve the result then place it into a mysldump.

Thanks
0
Comment
Question by:SaltyDawg
4 Comments
 
LVL 77

Assisted Solution

by:arnold
arnold earned 166 total points
ID: 22853948
variable =`echo "SELECT Updated FROM LastUpdate ORDER BY Updated DESC LIMIT 0, 1" | mysql -u mylogin -p mypass -h hostname -D database`
0
 
LVL 26

Accepted Solution

by:
ushastry earned 168 total points
ID: 22857659
Try this...
#!/bin/bash
 
 
v_tmp=`mysql -hlocalhost -umylogin -pmypass MyDb -e "SELECT Updated FROM LastUpdate ORDER BY Updated DESC LIMIT 0, 1;"`
 
UpdatedResult=`echo $v_tmp |cut -d ' ' -f2`
 
mysqldump -h hostname --user=mylogin --password=mypass --no-create-info --opt --where="LastUpdate>${UpdatedResult}" MyDb MyTable |sed -e "s|INSERT INTO|REPLACE INTO|" > MyDump_replace.sql

Open in new window

0
 
LVL 13

Assisted Solution

by:Xyptilon2
Xyptilon2 earned 166 total points
ID: 22857983
Here you go, just replace what needs to be inside the angular brackets.

#!/bin/bash

UpdatedResult=`mysql -u<USERNAME> -p<PASSWORD> -h<HOST> --skip-column-names --batch -D<DATABASE> -e 'SELECT Updated FROM LastUpdate ORDER BY Updated DESC LIMIT 0, 1;' `



0
 
LVL 1

Author Closing Comment

by:SaltyDawg
ID: 31512184
thanks all
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question