Escaping quote marks

Ray Parrish crp at cmc.net
Sat Nov 21 00:56:52 UTC 2009


Nils Kassube wrote:
> Ray Parrish wrote:
>   
>> Nils Kassube wrote:
>>     
>>> Maybe replace each space character of the URL with "%20"?
>>>       
>
>   
>> Ahhh, it just dawned on me... I bet I can replace the space
>>  characters with %20 as I load each line in from the file. I have no
>>  idea how to do that, as bash is not real string handling friendly,
>>  but I will work on it some, and see if I can figure out how to.
>>     
>
> Is this what you are looking for?
>
> URLs=""
> while read ThisLine; do
>   case "$ThisLine" in
>     *\<loc\>*)
>         while test "$ThisLine" != "${ThisLine#* }";do
>           x="${ThisLine#* }"
>           y="${ThisLine%$x}"
>           ThisLine="${y% }%20$x"
>         done
>         URLs="$URLs $ThisLine"
>     ;;
>   esac
> done < "$MapName"
>
>
> Nils
>   
Wow Nils, now if I could just understand what your code is actually 
doing. I did solve the problem with a function and word expansion. I 
pass each line to a function that receives the line as a parameter. If 
there are spaces in it, it gets treated like several command line 
parameters.

Here's my code. -

function ReplaceSpaces {
     # loop through command line parameters, concatenating them with %20
     # Assign the value of $1 to ThisLine
     ThisLine="$1"
     # shift command line parameters to the next one
     shift
     # if the command line arguments did not expire after the first 
shift, there must be spaces
     while [ "$1" != "" ]
          do
            # Concatenate ThisLine with %20, and the next command line 
argument
            ThisLine="$ThisLine%20$1"
            # shift to the next command line argument
            shift
     done
     return
}

And it gets called like so -

ReplaceSpaces $ThisLine

It works great, now all spaces in urls within my Google site map get 
replaced with %20, and the script runs just fine now.

Later, Ray Parrish

-- 
The Future of Technology.
http://www.rayslinks.com/The%20Future%20of%20Technology.html
Ray's Links, a variety of links to usefull things, and articles by Ray.
http://www.rayslinks.com
Writings of "The" Schizophrenic, what it's like to be a schizo, and other
things, including my poetry.
http://www.writingsoftheschizophrenic.com






More information about the ubuntu-users mailing list