简体   繁体   English

从bash到ash shell的转换:如何处理输入定义的数组?

[英]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. 我尝试将优秀示例docker-haproxy从centos转移到高山。

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. Shell脚本用于将作为脚本参数提供的值列表处理到数组中,然后将这些值及其索引写入某个文件。

The following construction works in bash: 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): 但不是灰烬(或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 bash到sh(灰)欺骗

sh apparently has no arrays. sh显然没有数组。

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 这是我需要继续的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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