mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <sha@pengutronix.de>
To: Frank Wunderlich <frank-w@public-files.de>
Cc: barebox@lists.infradead.org
Subject: Re: use string as variable name
Date: Tue, 25 Jan 2022 15:16:22 +0100	[thread overview]
Message-ID: <20220125141622.GC23490@pengutronix.de> (raw)
In-Reply-To: <trinity-82a62e55-a3b0-43a3-8656-8a983119c309-1642931916783@3c-app-gmx-bs30>

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


      reply	other threads:[~2022-01-25 14:18 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-23  9:58 Frank Wunderlich
2022-01-25 14:16 ` Sascha Hauer [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220125141622.GC23490@pengutronix.de \
    --to=sha@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=frank-w@public-files.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox