Linux

在 shell 腳本中定義數組

  • August 7, 2018

我正在嘗試在我的 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')

我究竟做錯了什麼?

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

引用自:https://serverfault.com/questions/925299