简体   繁体   中英

translation from bash to ash shell: how to handle arrays defined by input?

I try to transfer the excellent example docker-haproxy from centos to alpine.

A shell script is used to process a list of values given as parameters to the script into an array, then write these values plus their index to some file.

The following construction works in bash:

ServerArray=${SERVERS:=$1}
...
for i in ${ServerArray[@]}
do
  echo "   " server SERVER_$COUNT $i >> /haproxy/haproxy.cfg
  let "COUNT += 1"
done

but not in ash (or sh):

syntax error: bad substitution

The error refers to line

for i in ${ServerArray[@]}

What is the correct syntax here? I guess the line

ServerArray=${SERVERS:=$1}

does not define an array as intended, but googling for long did not help me.

bash to sh (ash) spoofing says

sh apparently has no arrays.

If so, how to solve the problem then?

I guess I can do with this construction:

#!/bin/sh
# test.sh
while [ $# -gt 0 ]
do
  echo $1
  shift
done

delivers

/ #  ./test 172.17.0.2:3306 172.17.0.3:3306
172.17.0.2:3306
172.17.0.3:3306

which is what I need to proceed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM