LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 11-30-2011, 08:19 AM   #1
slibbe
LQ Newbie
 
Registered: Nov 2011
Distribution: CentOS/Fedora/Ubuntu/Puppy
Posts: 5

Rep: Reputation: Disabled
Question mysql in bash script


Hi,

when I want to query some mysql database from bash, I can use the following construction as long as the query is simple:
QRY="$(mysql -u$USER -p$PASS $DB -Bse 'select * from table where userid=10')"

How do I manage to do something similar when the query itself allready has quotes? Like:
select * from table where created='2011-10-10 09:00:05' .
Now QRY="$(mysql -u$USER -p$PASS $DB -Bse 'select * from table where created=`2011-10-10 09:00:05` and userid=10')" doesn't work.

Regards,

slibbe
 
Old 11-30-2011, 08:58 AM   #2
corp769
Guru
 
Registered: Apr 2005
Posts: 5,807

Rep: Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996Reputation: 996
Instead of having 2011-10-10 09:00:05 in quotes, you should store it in a variable instead. Also, you used backticks in your example, instead of single quotes; Anything within backticks will be executed.
 
1 members found this post helpful.
Old 12-01-2011, 07:29 AM   #3
slibbe
LQ Newbie
 
Registered: Nov 2011
Distribution: CentOS/Fedora/Ubuntu/Puppy
Posts: 5

Original Poster
Rep: Reputation: Disabled
Code:
SEL="select username, articleid"
TBL="table"
WHC1="userid=10"
WHC2="created='2011-10-10 09:00:05'"
Q1="$SEL from $TBL"
Q2="where $WHC1 and $WHC2"
QRY="$Q1 $Q2"
echo $QRY
does echo
select username, articleid from table where userid=10 and created='2011-10-10 09:00:05'

However, a line like
Code:
mysql -u$USER -p$PASS $DB -e "$QRY"
or
Code:
mysql -u$USER -p$PASS $DB -e $QRY
doesn't work whereas the double quotes are substituted by single quotes in the first line and omitted in the second.
 
Old 12-02-2011, 08:37 AM   #4
slibbe
LQ Newbie
 
Registered: Nov 2011
Distribution: CentOS/Fedora/Ubuntu/Puppy
Posts: 5

Original Poster
Rep: Reputation: Disabled
Lightbulb

I got it!

Instead of
Code:
QRY="$(mysql -u$USER -p$PASS $DB -Bse 'select col1,col2,col3,datetime from table where col1=5 and datetime like '2010-04-01%' ')"
I can use
Code:
QRY=$(mysql -u$USER -p$PASS $db -e "select col1,col2,col3,datetime from table where col1=5 and datetime like '2010-04-01%'")
Don't know why the double quotes in the first line would be needed anyway. However if they are, you can do
'\''2010-04-01%'\'' instead of '2010-04-01%' (all single quotes).

Couldn't stand it not to find an answer for such a common problem

Last edited by slibbe; 12-02-2011 at 08:39 AM.
 
  


Reply

Tags
bash, mysql, quotes


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can MySQL log on via SSH/bash? mysql:x:27:101:MySQL Server:/var/lib/mysql:/bin/bash Ujjain Linux - Newbie 2 04-24-2009 02:21 PM
Is 'nice' inherited to child processes? e.g. bash script/php script that calls MySQL SirTristan Linux - Newbie 1 12-04-2008 12:57 AM
mysql insert using bash script venki Linux - General 3 07-07-2007 04:52 AM
Mysql in bash script acrors Red Hat 2 08-18-2004 09:11 PM
Bash script and mysql flashingcurser Programming 1 07-19-2004 03:35 AM


All times are GMT -5. The time now is 10:33 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration