gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:54:10 27 4
gpt4 key购买 nike

我试转优秀范例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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com