Solved

Run mysql select query from bash file

Posted on 2008-10-31
4
3,654 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
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 …
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

910 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now