我正在尝试在我的 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