Hallo Chuck! >> 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/[^=]*=//"` >> eval $var=\'$val\' >> echo "(1)" $var=\'$val\' >> echo "(2) myvar=$myvar" >> fi >> done >> echo "(3) myvar=$myvar" >> >> Which yields this: >> >> (1) myvar='my value' >> (2) myvar=my value >> (3) myvar= >> >> I am using Busybox v1.18.4 >> >> Any idea what is going on? > You will get the same results if you run that logic through bash. > The problem is that your eval logic only has effect until the loop > (everything after the pipe '|') ends.
In principle you are right, but it depends on the version of bash. There exist two strategies which lead to different results in those cases. Some shells run the last process in a pipe in the current shell others the first process. If the shell runs the first process of a pipe, all variable changes done by remaining processes of the pipe are lost. If the shell runs the last process of a pipe, variable changes in the last process are kept and others are lost. In my eyes that last case is the better choice ... but ash uses the other approach :-( Hints: If the variables are in usual shell syntax, why don't you source the file direct to grab the variable values? That way you can use all types of shell syntax for variable value definition, including substitutions, arithmetical expressions etc. [ -f YOUR_VAR_FILE ] && source YOUR_VAR_FILE ... or you can do ... while read opt; do # process your opt value here done <YOUR_VAR_FILE ... or another solution I'm using instead ... eval $( sed -e SCRIPT_TO_MODIFY_VAR_FILE YOUR_VAR_FILE ) -- Harald _______________________________________________ busybox mailing list [email protected] http://lists.busybox.net/mailman/listinfo/busybox
