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

Rasmus Villemoes rasmus.villemoes at prevas.dk
Tue Feb 18 09:11:26 CET 2020


On 16/02/2020 18.25, Wolfgang Denk wrote:
> Dear Rasmus,
> 
> In message <20200216152427.E80C7240036 at gemini.denx.de> I wrote:
>>
>> 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
> 
> Actually I'm stupid. 

No, but I did notice the above seemed needlessly obfuscated :)

 It's much easier this way, and without the
> ugly printed messages:
> 
> 	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 'setenv tmp_val $'BOOT_${i}_LEFT
> 	run tmp_cmd

Nice. So the trick I was missing was to get a literal $, followed by the
("computed") name of the env var I wanted, all embedded in a command to
be run (to invoke the second expansion).

It's a bit tricky, but it does get the job done. There should be some
catalogue of things like this, mentioning "U-Boot shell doesn't directly
have $that feature, but you can often emulate it with something like $this".

> 	test $slot = none && test $tmp_val -gt 0 && slot=$i && left=$tmp_val

If performance matters, one can move the tmp_cmd handling after the
slot=none test - then one can also use left directly instead of tmp_val,
so the line only grows by one clause:

 	test $slot = none && setenv tmp_cmd 'setenv left $'BOOT_${i}_LEFT &&
run tmp_cmd && test $left -gt 0 && slot=$i

Or as a more readable alternative that still avoids the "run" overhead
and saves one line (and the tmp_var)

        setenv tmp_cmd 'setenv left $'BOOT_${i}_LEFT
 	test $slot = none && run tmp_cmd && test $left -gt 0 && slot=$i

Thanks, Wolfgang. Consider both "env set -E" and the alternative "env
get" withdrawn.

Rasmus


More information about the U-Boot mailing list