mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* use string as variable name
@ 2022-01-23  9:58 Frank Wunderlich
  2022-01-25 14:16 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Frank Wunderlich @ 2022-01-23  9:58 UTC (permalink / raw)
  To: barebox

Hi,

is it possible to use a string as variable-name?

example:

i=1
img_$i=foo

now i have defined $img_1, but how to display it using $i again?

echo ${img_$i}
echo ${img_${i}}

alternative may be a variable-array, but it seems not possible

img[$i]=bar
img[1]=bar: No such file or directory

i just want to create a dynamic list and access the items of this list by index

another way may be appending new string with separator to existing string and split afterwards,
but then i have the index-problem again.

i know barebox shell is limited (but much more mighty than expected for just a bootloader),
but maybe it is possible :)

any idea?

regards Frank


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: use string as variable name
  2022-01-23  9:58 use string as variable name Frank Wunderlich
@ 2022-01-25 14:16 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2022-01-25 14:16 UTC (permalink / raw)
  To: Frank Wunderlich; +Cc: barebox

Hi Frank,

On Sun, Jan 23, 2022 at 10:58:36AM +0100, Frank Wunderlich wrote:
> Hi,
> 
> is it possible to use a string as variable-name?
> 
> example:
> 
> i=1
> img_$i=foo
> 
> now i have defined $img_1, but how to display it using $i again?
> 
> echo ${img_$i}
> echo ${img_${i}}
> 
> alternative may be a variable-array, but it seems not possible
> 
> img[$i]=bar
> img[1]=bar: No such file or directory
> 
> i just want to create a dynamic list and access the items of this list by index
> 
> another way may be appending new string with separator to existing string and split afterwards,
> but then i have the index-problem again.
> 
> i know barebox shell is limited (but much more mighty than expected for just a bootloader),
> but maybe it is possible :)

Nah, you're kidding, the barebox shell is not limited ;)

#!/bin/sh

list="foo bar baz"

index=$1

i=0
for elem in $list; do
        let i=$i+1
        if [ "$i" = "$index" ]; then
                result="$elem"
        fi
done

echo result: $result

Regards,
  Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-01-25 14:18 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-23  9:58 use string as variable name Frank Wunderlich
2022-01-25 14:16 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox