On Wed, May 25, 2011 at 4:24 PM, Nigel Hathaway
<[email protected]> wrote:
> I'm trying to import a set of variables from a file containing assignments of 
> the form "a=b", one on each line. I am finding that the variable assignments 
> are being lost.
>
> Here is a simplified version of my script which illustrates the problem:
>
> echo "myvar=my value" | while read opt; do
>    if echo "$opt" | grep -q '='; then
>        var=`echo "$opt" | /bin/sed "s/=.*//"`
>        val=`echo "$opt" | /bin/sed "s/[^=]*=//"`

This is inefficient (to run grep and two seds), and unsafe.

Efficiency:
if opt="a=b=c", then

var="${opt%%=*}"   #gives you "a" and
val="${opt#*=}"     #gives you "b=c"

Correctness:
if test x"$var" = x"$opt"; then <.... there is no '='....>

squote="'"
if test x"${val#*$squote}" != x"$val"; then <.... val contains single
quote, using '$val' won't be safe....>


> echo "myvar=my value" | while read opt; do
...
>        eval $var=\'$val\'
>        echo "(1)" $var=\'$val\'
>        echo "(2) myvar=$myvar"
>    fi
> done

"while ... done <FILE" will work.

-- 
vda
_______________________________________________
busybox mailing list
[email protected]
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to