LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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, 01: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, 02: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, 02:09 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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, 05: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 04:34 AM.
 
Old 06-04-2009, 11:21 PM   #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-04-2009 at 11:23 PM. Reason: OP already tried double quotes.
 
Old 06-05-2009, 12:05 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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-05-2009, 11:41 PM   #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, 05:27 AM   #8
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

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



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 06:22 AM
apt-get, driving me mad? luqman Red Hat 1 03-03-2005 12:03 AM
kernel is driving me mad luqman Red Hat 2 02-17-2005 07:00 AM
Samba driving me mad powadha Linux - Networking 4 06-10-2004 01:46 PM
mkhomedir driving me mad :-( datalord7 Linux - Newbie 2 01-21-2004 07:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:19 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
Open Source Consulting | Domain Registration