[PATCH] RFC: nvedit: support doing one (extra) expansion of the value in "env set"

Wolfgang Denk wd at denx.de
Sun Feb 16 16:24:27 CET 2020


Dear Rasmus,

In message <509a01fb-ba01-33b9-33bc-7323544d290a at prevas.dk> you wrote:
>
> So in bash, that might be written
> 
> slot=none
> for x in $BOOT_ORDER ; do
>   eval "left=\${BOOT_${x}_LEFT}"
>   if [ $left -gt 0 ] ; then
>     slot=$x
>     break
>   fi
> done

OK, now I get the context.

You mean something like this in U-Boot ?

	setenv BOOT_ORDER A B C D
	setenv BOOT_A_LEFT 0
	setenv BOOT_B_LEFT 0
	setenv BOOT_C_LEFT 2
	setenv BOOT_D_LEFT 5

	slot=none
	for i in $BOOT_ORDER ; do
	setenv tmp_cmd 'setexpr tmp_val sub '^' "" $'BOOT_${i}_LEFT
	run tmp_cmd
	test $slot = none && test $tmp_val -gt 0 && slot=$i
	done
	echo "## Chosen Slot = $slot"

This returns

	## Chosen Slot = C

as you want.  The only inelegance of this approach is that it will
also print

	tmp_val=0
	tmp_val=0
	tmp_val=2
	tmp_val=5

on the console, which is not really nice.  I wonder if it's worth
adding a "-q" flag to the setexpr sommand?

And - I even beat your bash script which has 8 lines, while my
script has only 6 :-) :-)

> Now we can work around the lack of break in the busybox shell by writing
> the loop so that the main body is skipped if we've found a valid slot:

Things like this are usually easier done using && and ||

> But we still can't translate this to busybox shell, because there's no
> eval. Now, I can do this with a hypothetical "env get" command which I
> just implemented to test that it works, and then the above becomes

There is no eval, but there is still a zillion of simple tricks you
can pull to get your stuff done :-)

> Now, if you can implement the line marked #magic with existing
> functions, I'm all ears. Or if you can implement the semantics of this
> snippet in some other way, which does not open-code explicit references
> to BOOT_A_LEFT, BOOT_B_LEFT etc. This is what I meant when I said I'd
> prefer not to write the loop like this:

See above...

> [yes, I also want left set as a side effect to the current value of the
> appropriate BOOT_x_LEFT].

Oh, this is a new requirement...

So lets change my little script to add setting "left":

	slot=none
	for i in $BOOT_ORDER ; do
	setenv tmp_cmd 'setexpr tmp_val sub '^' "" $'BOOT_${i}_LEFT
	run tmp_cmd
	test $slot = none && test $tmp_val -gt 0 && slot=$i && left=$tmp_val
	done
	echo "## Chosen Slot = $slot ; Left = $left"

Result:

	## Chosen Slot = C ; Left = 2

Best regards,

Wolfgang Denk

-- 
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Build a system that even a fool can use and only a fool will want  to
use it.


More information about the U-Boot mailing list