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

Re: .ini files in bash?



Once upon a time Cameron Hutchison said...
> 
> function savevars()
> {
>     for var in "$@" ; do
>         eval echo $var='$(echo $'$var' | sed -e "s/'\''/'\''\\\\'\'\''/g" -e "s/^/'\''/" -e "s/$/'\''/" )'
>     done
> }

That doesn't work properly either. Newlines in a variable are not
handled properly. I've got a new method that should handle anything and
not need to worry about quoting:

function savevars()
{
    set | awk -v varstr="$*" '
      BEGIN {gsub(/  */, "=|", varstr); vr="^(" varstr "=)"; vars=1} 
      / ()$/ {vars=0} 
      vars==1 && $0 ~ vr {print}
    '
}

This relies on the 'set' builtin which outputs variables in a form that
are output in a form that can be read by shell. It was mentioned before
but you said there was too much output.

This just puts an awk script after it to filter just the variable you
want.



Reply to: