![在 shell 脚本中定义数组](https://linux22.com/image/725561/%E5%9C%A8%20shell%20%E8%84%9A%E6%9C%AC%E4%B8%AD%E5%AE%9A%E4%B9%89%E6%95%B0%E7%BB%84.png)
我正在尝试在我的 shell 脚本中定义数组,其内容如下
cassandra_hosts=(cassandra1.test-dev.local cassandra2.test-dev.local cassandra3.test-dev.local)
但是它不允许我定义它并出现如下错误。
-bash: syntax error near unexpected token `cassandra1.test-dev.local'
我尝试了以下组合来定义它,但没有效果。
cassandra-hosts=('cassandra1.test-dev.local' 'cassandra2.test-dev.local' 'cassandra3.test-dev.local')
cassandra-hosts=("cassandra1.test-dev.local" "cassandra2.test-dev.local" "cassandra3.test-dev.local")
cassandra-hosts=(cassandra1\.test-dev\.local cassandra2\.test-dev\.local cassandra3\.test-dev\.local)
cassandra-hosts=('cassandra1\.test-dev\.local' 'cassandra2\.test-dev\.local' 'cassandra3\.test-dev\.local')
cassandra-hosts=("cassandra1\.test-dev\.local" "cassandra2\.test-dev\.local" "cassandra3\.test-dev\.local")
cassandra-hosts=("cassandra1\.test-dev\.local" "cassandra2\.test-dev\.local" "cassandra3\.test-dev\.local")
cassandra-hosts=('cassandra1\.test-dev\.local' 'cassandra2\.test-dev\.local' 'cassandra3\.test-dev\.local')
我究竟做错了什么?
答案1
bash-4.2$ cassandra_hosts="cassandra1.dev-internal.local cassandra2.dev-internal.local cassandra3.dev-internal.local"
bash-4.2$ for i in $cassandra_hosts; do echo "host: $i"; done
host: cassandra1.dev-internal.local
host: cassandra2.dev-internal.local
host: cassandra3.dev-internal.local