Ash eval wierdness
Denys Vlasenko
vda.linux at googlemail.com
Wed May 25 15:47:03 UTC 2011
On Wed, May 25, 2011 at 4:24 PM, Nigel Hathaway
<Nigel.Hathaway at ubiquisys.com> 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
More information about the busybox
mailing list