arrays - 从 bash 到 ash shell : how to handle arrays defined by input? 的翻译

标签 arrays linux bash shell alpine-linux

我试转优秀范例docker-haproxy从 centos 到 alpine。

shell 脚本用于将作为参数给定的值列表处理到数组中,然后将这些值及其索引写入某个文件。

bash 中的以下构建工作:

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

但不是在 ash(或 sh)中:

syntax error: bad substitution

错误指的是行

for i in ${ServerArray[@]}

这里正确的语法是什么?我猜线

ServerArray=${SERVERS:=$1}

没有按预期定义数组,但长时间谷歌搜索对我没有帮助。

bash to sh (ash) spoofing

sh apparently has no arrays.

如果是,那怎么解决呢?

最佳答案

我想我可以用这个结构来做:

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

交付

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

这是我需要继续的事情

关于arrays - 从 bash 到 ash shell : how to handle arrays defined by input? 的翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36264733/

相关文章:

调用 php 脚本结束时 PHP 后台作业停止

java - 如何使用 .bash rc 文件在 linux 中增加 java 的堆空间

linux - 与进程的双向通信

c++ - X11 窗口不会自动更新/绘图

python - 检测 python 脚本是从控制台还是通过 crontab 运行

javascript - 数组[字符串] OR 字符串到字符串

c - 在c中对二维数组进行排序

c - 二维数组的 malloc

javascript - 使用 Array.reduce() 将数组转换为对象

linux - 自动添加带空格的 SVN 命令行