LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 03-18-2004, 06:50 AM   #1
xianzai
Member
 
Registered: Oct 2003
Location: UK
Distribution: RHEL4
Posts: 54

Rep: Reputation: 15
BASH Shell Scripting Question


Hi all,

I have shell script that reads in a file, which is a string of text. That text makes up an SQL query later in the script.

How can I add slashes before each apostrophe so as not to make an SQL error?

EG:
This is what it could be....
Code:
MSG="This isn't my message, it's your msg"
This is what I want:
Code:
MSG="This isn\\'t my message, it\\'s your msg"
Just like the PHP function add_slashes()

Any idea anyone?

Thanks!
 
Old 03-18-2004, 06:58 AM   #2
fsbooks
Member
 
Registered: Jan 2002
Location: Missoula. Montana, USA
Distribution: Slackware (various)
Posts: 457

Rep: Reputation: 41
Add double backslashes:

$ cat /tmp/slash.bash
mess="This isn\\'t my message, it\\'s your msg"
echo $mess
$ /tmp/slach.bash
This isn\'t my message, it\'s your msg
 
Old 03-18-2004, 07:18 AM   #3
xianzai
Member
 
Registered: Oct 2003
Location: UK
Distribution: RHEL4
Posts: 54

Original Poster
Rep: Reputation: 15
Sorry, I don't understand what you mean.

I take in args $1 $2 etc

I want to replace all occurences of ' with \'

What does the cat /tmp/slash.bash do in your reply?

Thanks :-)
 
Old 03-18-2004, 07:33 AM   #4
snacky
Member
 
Registered: Feb 2004
Distribution: Debian
Posts: 286

Rep: Reputation: 30
Use:
Code:
sed "s/'/\\\'/g"
to do the text processing.
 
Old 03-18-2004, 02:13 PM   #5
naflan
Member
 
Registered: May 2003
Location: NC, USA
Distribution: Slackware 14.0
Posts: 94

Rep: Reputation: 15
~$ vi name_of_file

in vi type
Code:
:g/\\'/s/\\'/\\\'/g
make sure not in insert mode. If your are, press ESC.

Last edited by naflan; 03-18-2004 at 02:15 PM.
 
Old 03-19-2004, 07:50 AM   #6
xianzai
Member
 
Registered: Oct 2003
Location: UK
Distribution: RHEL4
Posts: 54

Original Poster
Rep: Reputation: 15
Thanks all, problem solved.
 
  


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
shell (bash) scripting question(s) boxerboy Programming 5 11-08-2005 03:51 PM
Bash shell scripting question. pete1234 Programming 11 09-25-2005 02:59 AM
Bash shell scripting question. Itsu Linux - General 3 04-30-2005 03:52 AM
Bash shell scripting question mehesque Programming 2 07-15-2004 10:54 AM
Help with I/O on bash shell scripting Dave6383 Programming 1 06-03-2004 05:24 PM

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

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