Ash array : not found errors ,string concatenation from an array

i am tring to work with Ash arrays

#!/bin/ash
 myarray[0]=hello
 myarray[1]=world

but i am getting this error messages

/root/qtest.sh: line 2: myarray[0]=hello: not found
/root/qtest.sh: line 3: myarray[1]=world: not found

also tried like this but still the same

 myarray[0]="hello"
 myarray[1]="world"

where can i find proper way to work with ash arrays ?

i have a file /root/strings

Hello
World
it 
is
me

i need to concatenate all the strings from this file to variable

resultstring = "[Hello], [World ] ,[it] ,[is] ,[me]"
file can be diffrent any numbers of strings

Maybe this is useful?

Note this is not an OpenWRT specific question so I doubt that this is the appropriate forum for these questions.

that is bash not ash but anyway i tried

myArray=("cat" "dog" "mouse" "frog")

for i in ${!myArray[@]}; do
  echo "element $i is ${myArray[$i]}"
done

output

/root/qtest.sh: line 3: syntax error: unexpected "("
root@AP3:~#

also tried like this

myArray="cat dog mouse frog"

for i in ${!myArray[@]}; do
  echo "element $i is ${myArray[$i]}"
done

output:

root@AP3:~# /root/qtest.sh
/root/qtest.sh: line 7: syntax error: bad substitution
root@AP3:~#

Sorry if you want to use it on the router directly which is using ash then you are out of luck as ash does not has arrays as far as I know.

You can use a string with a delimiter instead to mimic an array

3 Likes

can always install bash ...

3 Likes

if i use this kind of contraction i can get each element from the array

#!/bin/ash
    reply="this is a reply message that "
    for i in $reply
    do
    echo "word: $i"
    done

output

root@AP3:~# /root/qtest.sh
word: this
word: is
word: a
word: reply
word: message
word: that
root@AP3:~#

is there any way i can make each interaction variable like separate variable so then i can make concatenation
without using array variable?

200 Kb left on router no space for additional packages

Require approx. 768.96 KiB size for 5 package(s) to install bash

You can do more sophisticated string and array processing by invoking awk or sed, both of which are built into a default build.

2 Likes
reply="this is a reply message that "
y=0;for i in $reply; do echo "word: $i"; y=$((y+1)); eval string$y="$(echo $i)";done

root@DL-WRX36:~# echo $string5
message

Perhaps this is working?

1 Like

yes it works , now i need to try it in my task thanks a lot i am trying now

root@DL-WRX36:~# for p in $(seq 1 $y); do eval echo \$string$p;done
this
is
a
reply
message
that
root@DL-WRX36:~#

Be real careful with eval. If an untrusted source is supplying the input strings, they can be easily crafted to make your script execute arbitrary code. eval literally means "Execute this arbitrary code..."

3 Likes
reply="this is a reply message that "

y=0;
for i in $reply; 
do 
echo "word: $i";
 y=$((y+1)); 

eval string$y="$(echo $i)";

done



echo \string$y $string2 $string3

root@AP3:~# /root/qtest.sh
word: this
word: is
word: a
word: reply
word: message
word: that
string6 is a
root@AP3:~#

how can i generate variables name for output ?
i can get number of strings so maybe i can generate them in output

Like in my earlier post?

1 Like

Sorry i am writing the code didnt understand it but thanks it works!
and the last thing to do is to concatenate all of them but it supposed to be a string like this

echo " [[ $string1 ] ,[$string2], [$string3]] "

output shoud be like this string

[[this],[is],[a]]

that is the thing i need to make special string from a list of strings
and the number if strings can be diffrent
i know a way but that way is dummy

#!/bin/ash
# Parse the first 3 words (elements) of a string and format them with bracket and comma
# separators like this "[[ELEMENT1],[ELEMENT2],[ELEMENT3]]

# test string
INPUT="this is a reply message that"

OUTPUT="[["
COUNT=0
for ELEMENT in $INPUT; do
        OUTPUT="$OUTPUT$ELEMENT]"
        COUNT=$((COUNT+1))
        [ $COUNT -eq 3 ] && break
        OUTPUT="$OUTPUT,["
done
OUTPUT="$OUTPUT]"

echo $OUTPUT

results in:
[[this],[is],[a]]

4 Likes

that is a pure golden code :grinning:
i replace 3 with $y and all works as it was expected
for me that is an other level of coding
Thanks a lot !! :+1:

and now i need this output

[this,is,a]

i am trying to modify your code but a brecket keeps missing
oh i did it :grinning:

#!/bin/ash

# Parse the words (elements) of a space-separated string into an "array definer"
# like this: "[ELEMENT1,ELEMENT2,ELEMENT3]"
# only the first $LIMIT elements will be copied.  If LIMIT=0, the whole string
# is processed.

INPUT="this is a reply message that"
LIMIT=0

OUTPUT="["
COUNT=0
for ELEMENT in $INPUT; do
        [ $COUNT -gt 0 ] && OUTPUT="$OUTPUT,"
        OUTPUT="$OUTPUT$ELEMENT"
        COUNT=$((COUNT+1))
        [ $COUNT -eq $LIMIT ] && break
done
OUTPUT="$OUTPUT]"

echo $OUTPUT

This new version works standalone, it is not necessary to count the number of elements (former y variable) first.

2 Likes

SUSv4 shell syntax and specifications.

2 Likes