[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: .ini files in bash?



Once upon a time Cameron Hutchison said...
> 
> I was working on a proper solution, but put it aside when I got to nine
> consecutive backslashes in a sed expression.

Replying to my own posts - a sign of madness.

A further sign is that I think I've got a function that will do it
properly. I didn't need nine consecutive backslashes, but it's still not
pretty.

> If anyone feels like an exercise, the goal is to get a file that can be
> sourced by the shell that contains lines of the form:
> 
> FOO='bar'
> 
> with single quotes surrounding the value. You need to handle the case
> where there is a single quote in the string:
> 
> The string
> World's apart. Who's the boss.
> 
> needs to be encoded as:
> 
> FOO='World'\''s apart. Who'\''s the boss.'

function savevars()
{
    for var in "$@" ; do
        eval echo $var='$(echo $'$var' | sed -e "s/'\''/'\''\\\\'\'\''/g" -e "s/^/'\''/" -e "s/$/'\''/" )'
    done
}

Make sure you have that eval line as a single line if your mailer wraps
it. It may be a a little hard to discern the single quotes from the
double quotes from the backquotes so:

1) There are no backquotes above.
2) The only double quotes are around the sed expressions (-e "s/a/b/").
   And there's double quotes arounf "$@" on the line before.
3) The others are all single quotes - no more than two consecutive
   single quotes.



Reply to: