Solved

Run mysql select query from bash file

Posted on 2008-10-31
4
3,660 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 78

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:
Umesh 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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
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.

688 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