LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 07-08-2009, 02:09 PM   #1
tirengarfio
Member
 
Registered: Nov 2006
Posts: 45

Rep: Reputation: 15
Bash with MySQL sintax


Hi,

i have this script below. When i execute it, i get these errors:

Code:
89.17.206.129
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.206.129
)' at line 5
89.17.206.130
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.206.130
)' at line 5
89.17.206.131
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.206.131
)' at line 5

I dont get any error when i write manually at mysql prompt this:

Code:
mysql> INSERT INTO `pingp22`.`ip` ( `ip` ) VALUES (  "89.17.206.129" );
This is the bash code:

Code:
IPS="$(seq -f "89.17.206.%.0f" 129 189) $(seq -f "89.17.219.%.0f" 65 126)"

for i in ${IPS}
do
if [ -z "$(nmap -p 22 $i |grep closed)" ]; then

BD="pingp22" 

echo $i

mysql -uroot -pm -D $BD -Bse 'INSERT INTO `pingp22`.`ip` (

`ip`
)
VALUES (
 '$i'
);
'

fi
done
Any idea?


Ciao
 
Old 07-08-2009, 02:33 PM   #2
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 46
Moin,

you don't quote the $i variable. Your created sql command is not equal to the one you start at the mysql prompt. Add double quotation marks, then it should work.

It's always a good idea to "echo" such commands when you are receiving an error. A "set -x" at the beginning of the script can help too.

Jan

EDIT: the critical lines in the script should look like this:
Code:
...
VALUES (
 "'$i'"
);
...

Last edited by jan61; 07-08-2009 at 05:46 PM.
 
Old 07-08-2009, 06:51 PM   #3
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,236

Rep: Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545
set -x in the script, or running the script
Code:
sh -x /path/to/my/script
is almost mandatory when running Bash scripts for the first time.

I wonder if quoting the $i is done in the correct way. '$i' will not be expanded by Bash and will be echoed as $i

Jan is right that "'$i'" again is expanded and will show 'value-of-i' including the single qoutes. But it is still tricky and prone to errors.

This is my standard way:
echo "UPDATE foo SET bar = \"$thit\" WHERE day = \"new\" "| mysql -u jlinkels -pmypass mydatabase.

It is a good idea to escape characters as '(' as well if you are not sure whether they mean something to Bash.

Escaping quotes like this never gives me any problems or confusions in SQL statements, and you avoid qouting hell if you want to include a single or double qoute in one of the SQL values.

jlinkels
 
  


Reply

Tags
bash, mysql, networking


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
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 03:21 PM
Bash - Mysql chriskippi Programming 1 11-16-2008 05:59 PM
mysql and bash rharris72 Programming 1 12-24-2005 07:01 AM
Crontab sintax sarajevo Debian 4 09-05-2005 09:52 AM
Bash + Mysql Help xianzai Programming 4 10-28-2004 09:15 AM


All times are GMT -5. The time now is 12:33 PM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration