From: mulima on
Hi

i didn't succeded in replacing a single quote by an escaped one :-/

i do :


$ a=`cat myfile`
$ echo 'insert into mytable (myfield) values ('$a');" | mysql


the probleme is that 'myfile' can contain single quote not escaped..
like

myfile=rock'n' roll is very good music !


does anyone could help me ?

regards ;-)
mulima


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Article poste via Voila News - http://www.news.voila.fr
Le : Mon Sep 5 18:50:31 2005 depuis l'IP : 82.125.107.96 [VIP 210319158618]
From: Loki Harfagr on
Le Mon, 05 Sep 2005 16:50:32 +0000, mulima a ýcritý:

> Hi
>
> i didn't succeded in replacing a single quote by an escaped one :-/
>
> i do :
>
>
> $ a=`cat myfile`
> $ echo 'insert into mytable (myfield) values ('$a');" | mysql
>
>
> the probleme is that 'myfile' can contain single quote not escaped..
> like
>
> myfile=rock'n' roll is very good music !
>
>
> does anyone could help me ?
>
Is that it or did I miss the question ?

$ echo "insert into mytable (myfield) values ('\$$a');"
insert into mytable (myfield) values ('$myfile');

From: Chris F.A. Johnson on
On 2005-09-05, mulima wrote:
> Hi
>
> i didn't succeded in replacing a single quote by an escaped one :-/
>
> i do :
>
>
> $ a=`cat myfile`
> $ echo 'insert into mytable (myfield) values ('$a');" | mysql
>
>
> the probleme is that 'myfile' can contain single quote not escaped..
> like
>
> myfile=rock'n' roll is very good music !

Doesn't this work?

echo "insert into mytable (myfield) values (\"$a\");" | mysql

--
Chris F.A. Johnson <http://cfaj.freeshell.org>
==================================================================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>
From: Chapman Flack on
Chris F.A. Johnson wrote:
> echo "insert into mytable (myfield) values (\"$a\");" | mysql

That changes the OP's problem from values of $a that contain ' to
values
that contain ".

The fundamental issue is that a shell script is being used to generate
text
that will be scanned by the rules of another language (here mysql), and
it is
necessary to learn the rules of /the other language/, and write the
script
to properly convert values into the form that language needs. For this
particular example, the rules are here:

http://dev.mysql.com/doc/mysql/en/string-syntax.html

>From them one learns that it will be necessary to escape both the quote
character and the backslash character. Preceding either one with a
backslash
is sufficient. In the Korn shell, this would be a solution:

print -r "insert into mytable (myfield) values
('${a//([\'\\])/\\\1}');"|mysql

Try and see what happens if you leave off the -r.

From: mulima on
Chapman Flack a ýcrit :
> Chris F.A. Johnson wrote:
>
>>echo "insert into mytable (myfield) values (\"$a\");" | mysql
>
>
> That changes the OP's problem from values of $a that contain ' to
> values
> that contain ".
>
> The fundamental issue is that a shell script is being used to generate
> text
> that will be scanned by the rules of another language (here mysql), and
> it is
> necessary to learn the rules of /the other language/, and write the
> script
> to properly convert values into the form that language needs. For this
> particular example, the rules are here:
>
> http://dev.mysql.com/doc/mysql/en/string-syntax.html
>
>>From them one learns that it will be necessary to escape both the quote
> character and the backslash character. Preceding either one with a
> backslash
> is sufficient. In the Korn shell, this would be a solution:
>
> print -r "insert into mytable (myfield) values
> ('${a//([\'\\])/\\\1}');"|mysql
>
> Try and see what happens if you leave off the -r.
>


Hi :)

it seem to me that this solution could be the good one, but print -r
does not work on my shell :

$ print -r "insert into mytable (myfield) values ('${a//([\'\\])/\\\1}');"

Warning: unknown mime-type for "-r" -- using "application/*"
Error: no such file "-r"
Error: no such file ""

i'm a noob .. so i just know that i use a debian derived distribution
(ubuntu) but i don't really know what shell i use... is "bash" give you
enough informations to help me a bit more :-) ?

and for sure mysql really need to escaped various special chars as you
mentionned it ..

regards mulima