LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-04-2009, 02:53 PM   #1
CopyrightPhilly
LQ Newbie
 
Registered: Feb 2006
Location: UK
Posts: 27

Rep: Reputation: 15
bash, mysql & backquote driving me mad


Hello chaps,

ok im trying to make an automated setup script to setup a server.

Now when trying to execeute a mysql command i keep getting errors due to one of the values needed backquotes.

for example:
mysql -D WS --password="whatever" -u root -e "CREATE TABLE example (`sqlquery` text NOT NULL);"

sqlquery needs to be inside backquotes because of its name, its the only mysql command thats doing it and its driving me mad! so, how can i get this to work? if i run the about i get "sqlquery command not found" iv tried using \`sqlquery\` but no joy.

can anyone help me with this?

thanks
 
Old 06-04-2009, 03:09 PM   #2
grepmasterd
Member
 
Registered: Aug 2003
Location: Seattle
Distribution: ubuntu, lately
Posts: 182
Blog Entries: 1

Rep: Reputation: 35
try single quotes?

mysql -D WS --password="whatever" -u root -e 'CREATE TABLE example (`sqlquery` text NOT NULL);'

from the bash man page:
Quote:
Enclosing characters in single quotes preserves the literal value of
each character within the quotes
 
Old 06-04-2009, 03:09 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
What if you put single quotes around the argument of -e?
Code:
mysql -D WS --password="whatever" -u root -e 'CREATE TABLE example (`sqlquery` text NOT NULL);'
They should protect back quotes from command substitution in bash.

Edit: beaten by grepmasterd for a bunch of seconds!
 
Old 06-04-2009, 06:06 PM   #4
soleilarw
Member
 
Registered: Apr 2009
Posts: 107

Rep: Reputation: 19
The single quotes variant did work on my Linux box:
mysql -D test --password="" -uroot -e 'CREATE TABLE example (`sqlquery` text NOT NULL);'
Though I don't understand why sqlquery needs backticks at all. On my system it becomes simply the fieldname. Am I missing something peculiar to mySQL?

Linux Archive

Last edited by soleilarw; 06-18-2009 at 05:34 AM.
 
Old 06-05-2009, 12:21 AM   #5
rikxik
Member
 
Registered: Dec 2007
Posts: 88

Rep: Reputation: 19
Can you please cut paste the entire command AND the error from console.

Last edited by rikxik; 06-05-2009 at 12:23 AM. Reason: OP already tried double quotes.
 
Old 06-05-2009, 01:05 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
The general is you only need backticks for a col name if it is also an SQL reserved word or has spaces in it or other special chars.
Personally I avoid those like the plague, as I also avoid spaces etc in filenames... more trouble than they're worth.
 
Old 06-06-2009, 12:41 AM   #7
rikxik
Member
 
Registered: Dec 2007
Posts: 88

Rep: Reputation: 19
The only reason you would include backticks on a shell command is to substitute the output. E.g.:

Code:
$ echo "The time now is: `date`"
The time now is: Sat Jun  6 12:35:36 SGT 2009
I thought the reason you used backticks i.e. `sqlquery` was that it was some sort of script which would return the column name. That doesn't seem to be the case (and thats why you got the "sqlquery command not found").

I can't see why enclosing it in single-quote/double-quote/backticks is needed because "sqlquery" is not a mysql reserved word (as per this) and its not a reserved word for the shell.

What happens if you simply run it like this?

Code:
mysql -D WS --password="whatever" -u root -e "CREATE TABLE example (sqlquery text NOT NULL);"
 
Old 06-06-2009, 06:27 AM   #8
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Not an answer to the question but you can also put all SQL commands in one file and use
Code:
mysql -u root -pxxx < myfile
This might be more efficient than running a number of individual SQL statements through bash (and I personally consider it cleaner).

If there is no need for username and/or password, you can leave the relevant options out. I would anyway not put the password in the script for security reasons but use -p instead of -pxxx.
 
  


Reply


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
battstat driving me mad oookiezooo Linux - Newbie 1 04-15-2005 07:22 AM
apt-get, driving me mad? luqman Red Hat 1 03-03-2005 01:03 AM
kernel is driving me mad luqman Red Hat 2 02-17-2005 08:00 AM
Samba driving me mad powadha Linux - Networking 4 06-10-2004 02:46 PM
mkhomedir driving me mad :-( datalord7 Linux - Newbie 2 01-21-2004 08:09 AM


All times are GMT -5. The time now is 09:47 AM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration