Run mysql select query from bash file

Posted on 2008-10-31
Medium Priority
Last Modified: 2012-05-05
I need to run a select query in bash script and pass the result to a variable



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.

Question by:SaltyDawg
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
LVL 79

Assisted Solution

arnold earned 664 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`
LVL 26

Accepted Solution

Umesh earned 672 total points
ID: 22857659
Try this...
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

LVL 13

Assisted Solution

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


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;' `


Author Closing Comment

ID: 31512184
thanks all

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

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…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Suggested Courses

770 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