LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 04-30-2004, 10:36 PM   #1
Helene
LQ Newbie
 
Registered: Apr 2004
Posts: 19

Rep: Reputation: 0
Handle escape characters in a string


echo -n "Please enter filename(s) in regular expression: "

read -r option # assume that the user typed in: .*\.txt
echo $option # prints out .*\.txt

How can I change the content of $option to be .*\\.txt?
Why can't I just do:
echo $option | tr '\' '\\' ?

I appreciate any help!
-Helene
 
Old 04-30-2004, 10:57 PM   #2
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Code:
#!/bin/bash
echo -n "Please enter filename(s) in regular expression: "
read -r option
echo $option | sed 's/\\/\\\\/'
Not sure what you're doing, but will that work? I tend not to use tr, but sed will do it if you escape the backslash and then escape both replacements.
 
Old 04-30-2004, 11:14 PM   #3
Helene
LQ Newbie
 
Registered: Apr 2004
Posts: 19

Original Poster
Rep: Reputation: 0
Excellent, digiot! Now I'm soon ready to deliver my Uni assignment
 
Old 04-30-2004, 11:20 PM   #4
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Uni assignment? Argh. I can usually tell when it's a homework thing, but I didn't realize it in this case. You do know you're not supposed to ask for homework help, right? I mean, you can ask about principles and I or others would offer hints and principles, but just straight questions and answers are kind of a no-no.

Ah well, bit past the point now. Good luck with the classwork, anyway.
 
Old 05-01-2004, 04:23 AM   #5
Helene
LQ Newbie
 
Registered: Apr 2004
Posts: 19

Original Poster
Rep: Reputation: 0
Well, I'm only asking about principles, digiot. I would never have asked for direct answers when doing assignments! If you are still willing to help me, I have one more question:

test=`echo $option | sed 's/\\/\\\\/''`

This results in Unterminated `s' command. Why?
I did find another way aronud (not pretty), but I'm just curious
 
Old 05-01-2004, 02:16 PM   #6
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Actually, what you posted has two single quotes, so gets 'unexpected EOF' for me, but
Code:
test=`echo $option | sed 's/\\/\\\\/'`
does get 'unterminated'.

The original 'backticks' method is treated differently and the newer way works:
Code:
test=$(echo $option | sed 's/\\/\\\\/')
Near as I can figure, the backtick method is resulting in the backslashes getting 'swallowed', and maybe the slashes being treated by sed as literal - so it's still waiting on a 'special' slash to terminate the substitution.
Code:
~
990>> echo `echo /\\/\\\\/`                                                   
//\/

~
991>> echo $(echo /\\/\\\\/)                                                  
/\/\\/
In the first, bash is passing to sed what looks like 'substitute nothing with a literal slash and...' whereas the second is 'replace a slash with two'. Now, what bash is passing to sed and what echo is outputting in 990/991 are two different things but I still think that's basically what's going on. Could well be wrong, though. It's not that I'm not willing to help, but I just don't really know.

Just out of curiosity, what was the way around? (If you don't want to say, that's okay. I, too, know the shame of ugly workarounds. )
 
Old 05-01-2004, 11:24 PM   #7
Helene
LQ Newbie
 
Registered: Apr 2004
Posts: 19

Original Poster
Rep: Reputation: 0
This one seems to be fine! I didn't know/think of using $().
test=$(echo $option | sed 's/\\/\\\\/')

And yes, I had a typo. I didn't mean to write two single quotes, only one.

My ugly solution was to write the variable to a file, and write it back into a variable using awk...
echo $option | sed 's/\\/\\\\/' > tmpFile .....
 
Old 05-01-2004, 11:43 PM   #8
slakmagik
Senior Member
 
Registered: Feb 2003
Distribution: Slackware
Posts: 4,113

Rep: Reputation: Disabled
Yeah, I try to avoid temp files but sometimes it does seem like the least troublesome route. Glad the other suits.
 
  


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
passing escape characters along with lp command zita Linux - General 0 10-05-2005 01:58 PM
Variables and escape characters? flobadon Programming 4 09-10-2005 04:34 PM
bach scripting and escape characters... Bud-froggy Linux - Newbie 3 07-09-2004 04:55 PM
Escape Characters in linux shunraj Linux - Software 1 05-18-2004 03:21 PM
escape characters not escaping BobNz Linux - Software 2 04-09-2004 03:34 AM

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

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