How to use array in ash?
Asked Answered
E

2

9

For some reason, I can't use bash to build my script, the only way to do it is with ash, I have this sms auto responder script, each reply must be at max 160 chacters long, it looks like this:

#!/bin/sh
    reply="this is a reply message that contains so many words so I have to split it to some parts"
    array="${reply:0:5} ${reply:5:5} ${reply:10:5} ${reply:15:5} ${reply:20:5}"
    for i in $array
    do
    echo "send sms with the message: $i"
    done

but it ends up like this:

send sms with the message: this
send sms with the message: is
send sms with the message: a
send sms with the message: reply
send sms with the message: mess
send sms with the message: age
send sms with the message: t

What I want is something like this:

send sms with the message: this 
send sms with the message: is a 
send sms with the message: reply
send sms with the message: messa
send sms with the message: ge th
send sms with the message: at co
send sms with the message: ntain

So it splits by the number of characters instead of splitting by words, how can I do that?

Erbium answered 27/3, 2015 at 7:18 Comment(0)
Q
9

Your array is not actually an array, but read http://mywiki.wooledge.org/WordSplitting for more info on that.

ash does not natively support arrays, but you can circumvent that by using set:

reply="this is a reply message that contains so many words so I have to split it to some parts"
set -- "${reply:0:5}" "${reply:5:5}" "${reply:10:5}" "${reply:15:5}" "${reply:20:5}"
for i; do
        echo "send sms with the message: $i"
done

-

send sms with the message: this
send sms with the message: is a
send sms with the message: reply
send sms with the message:  mess
send sms with the message: age t

There are many alternative solutions to this, here's one of them using fold to do the splitting work for you with the added advantage that it can break on spaces to make the messages a bit more readable (see man fold for more info):

reply="this is a reply message that contains so many words so I have to split it to some parts"
echo "${reply}" | fold -w 10 -s | while IFS= read -r i; do
        echo "send sms with the message: $i"
done

-

send sms with the message: this is a
send sms with the message: reply
send sms with the message: message
send sms with the message: that
send sms with the message: contains
send sms with the message: so many
send sms with the message: words so
send sms with the message: I have to
send sms with the message: split it
send sms with the message: to some
send sms with the message: parts
Quadrature answered 27/3, 2015 at 7:34 Comment(0)
R
-1

sh and ash, just as bash does, "support" arrays through strings. When you declare a variable as a string with quotes and leave space in-between you can loop through the items in the array with a for loop.

#! /bin/sh

export SOME_VAR="something in between with,a,comma as an example"
for i in $SOME_VAR; do echo "This is an element of the array: $i"; done

And here is the output:

This is an element of the array: something
This is an element of the array: in
This is an element of the array: between
This is an element of the array: with,a,comma
This is an element of the array: as
This is an element of the array: an
This is an element of the array: example
Roley answered 21/11, 2022 at 10:46 Comment(1)
doesn't this cause globs like * to be expanded?Jaxartes

© 2022 - 2024 — McMap. All rights reserved.