OpenWrt Forum Archive

Topic: arrays in shell script?

The content of this topic has been archived on 30 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Does the version of the bash shell used in busybox have array support compiled in?  I'm trying to convert a script of mine over to run on my wrt but am having issues, and could use a working example if someone has something.  Specifically, I'm needing to set up an array of strings containing URLs.

Thanks in advance!

ash does not support arrays. use awk

Hi Jim,
This works for me-

#!/bin/ash

ARRAY=" \
http://openwrt.org \
http://wiki.openwrt.org \
"
i=1

for j in $ARRAY
do
        echo $i: $j
        i=`expr $i + 1`
done

Hope this helps,
Charlie

Thanks all.

ciscostu wrote:

Hi Jim,
This works for me-

#!/bin/ash

ARRAY=" \
http://openwrt.org \
http://wiki.openwrt.org \
"
i=1

for j in $ARRAY
do
        echo $i: $j
        i=`expr $i + 1`
done

Hope this helps,
Charlie

Sorry I have Question about your array , If i want to show "http://wiki.openwrt.org " only how can i do ?

how can i specify to show one value of your array?

ps. Sorry to you , if you confuse my english

(Last edited by Backbite on 14 Jul 2012, 16:29)

No one to help me?

Backbite wrote:
ciscostu wrote:
ARRAY=" \
http://openwrt.org \
http://wiki.openwrt.org \
"

how can i specify to show one value of your array?

Unfortunately, that is not a true array, so you cannot specify one value within it. It is actually nothing but a list, or to be more precise, it is a string which is broken into a list by the "for" line in the example script.

Bash has arrays, but ash does not.
http://tldp.org/LDP/abs/html/arrays.html

- Win

The discussion might have continued from here.