Solved

Run mysql select query from bash file

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

Assisted Solution

by:arnold
arnold earned 166 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
thanks all
0

Featured Post

Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

Join & Write a Comment

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 …
SSH (Secure Shell) - Tips and Tricks As you all know SSH(Secure Shell) is a network protocol, which we use to access/transfer files securely between two networked devices. SSH was actually designed as a replacement for insecure protocols that sen…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

762 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

8 Experts available now in Live!

Get 1:1 Help Now