LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-17-2006, 07:24 AM   #1
d60eba
LQ Newbie
 
Registered: Jul 2006
Posts: 3

Rep: Reputation: 0
Bash script - how to add slashes to quotations for sending to MySQL?


Hi all,

I have the following script:

Code:
thedate=$(date +"%Y-%m-%d")
thetime=$(date +"%H:%M:%S")

if [ "$(GET domain | grep 'Welcome')" ]
then echo "INSERT INTO hc_checks ( id_check ,date , time , status)
VALUES (
'','$thedate', '$thetime', '1'
);" | mysql --user --pass=*** host_check
else
page_text=$(GET domain)
echo "INSERT INTO hc_checks ( id_check ,date , time , status, page_text)
VALUES (
'','$thedate', '$thetime', '0', '$page_text'
);" | mysql --user --pass=*** host_check
fi
A simple script that checks the homepage for the text string "Welcome". If it doesn't find the string it inserts the page it does find into a MySQl database.

The problem is that the string may contain " and ' characters which MySQL doesn't like. In PHP I would solve this problem by:

$page_text= addslashes($page_text); but I have no idea how to do this in Bash. I can't even work out how to find/replace!

Any help greatly appreciated.

Leon
 
Old 07-17-2006, 07:36 AM   #2
leonscape
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Debian SID / KDE 3.5
Posts: 2,313

Rep: Reputation: 48
The best place to look for these answers is in Advanced Bash Scripting Guide

Find and replace is
Code:
${string/substring/replacement}	        Replace first match of $substring with $replacement
${string//substring/replacement}	Replace all matches of $substring with $replacement
I think you can just escape quotes with a backslash (i.e. \" ) but you'd have to double check as I stuck on windows at work at the moment.
 
Old 07-17-2006, 08:25 AM   #3
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Is it the $page_text where you want to escape quotes?

Because there are two types of quotes to escape, you could do it like this:
page_text="${page_text//\'/\'}"
page_text="${page_text//\"/\\\"}"

or page_text=$(GET domain | sed 's/['"'"'"]/\\&/g')
Notice the sed command. MySQL isn't alone in not liking embedded quotes. I had to break up the sed command so that the shell wouldn't balk.

Here I'll add some extra spaces so that it is clearer:
Code:
sed 's/['     "'"     '"]/\\/&/g'
Embedding single quotes in a bash string is the tricky part.

example demo:
Code:
jschiwal@hpamd64:~> GET domain
Welcome. This is a "sample" test.  Let's see another sentence.
jschiwal@hpamd64:~> GET | sed 's/['"'"'"]/\\&/g'
Welcome. This is a \"sample\" test.  Let\'s see another sentence.
Code:
jschiwal@hpamd64:~> page_text="$(GET domain)"
jschiwal@hpamd64:~> page_text="${page_text//\'/\'}"
jschiwal@hpamd64:~> page_text="${page_text//\"/\\\"}"
jschiwal@hpamd64:~> echo $page_text
Welcome. This is a \"sample\" test. Let\'s see another sentence.

Last edited by jschiwal; 07-17-2006 at 08:39 AM.
 
Old 07-17-2006, 10:36 AM   #4
d60eba
LQ Newbie
 
Registered: Jul 2006
Posts: 3

Original Poster
Rep: Reputation: 0
Guys - thanks very much for the help there. Some very tricky syntax!
 
Old 07-17-2006, 02:16 PM   #5
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Heh, you can actually just use php if you'd like to,
Code:
#!/usr/bin/php
<?php
	while($str = fgets(STDIN)) {
		echo(addslashes($str));
	}
?>
Save it to whatever name you like, then either make it executable (chmod 755 xxx.php for instance), or run it via php -f xxx.php, where xxx.php is the filename. Pipe it some MySQL command (generate-sql | xxx.php), and get back the slashified version.
 
  


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
how to add numbers in a formatted file via a bash script? zero79 Linux - General 8 12-24-2010 05:48 PM
add user bash script noir911 Programming 4 08-13-2005 08:24 AM
bash and double slashes MD3 Linux - Software 0 02-18-2005 03:13 PM
Bash script and mysql flashingcurser Programming 1 07-19-2004 03:35 AM
Bash script: add all numbers from command output wi-Z-art Programming 2 08-06-2003 09:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:13 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