我有一个内容文件:
( [datname]=template1 [datctype]=cs_CZ.utf-8 ) ( [datname]=template0 [datctype]=cs_CZ.utf-8 ) ( [datname]=postgres [datctype]=cs_CZ.utf-8 ) ( [datname]=some\ stupid\ name [datctype]=cs_CZ.utf-8 ) ( [datname]=jqerqwer,\ werwer [datctype]=cs_CZ.utf-8 )
我想读取每一行并将上下文推送到关联数组变量.以下代码我没有成功:
(cat <<EOF ( [datname]=template1 [datctype]=cs_CZ.utf-8) ( [datname]=template0 [datctype]=cs_CZ.utf-8 ) EOF ) | while read r do declare -A row=("$r") echo ${row[datname]} done;
我收到一个错误:
test3.sh: line 8: row: ( [datname]=template1 [datctype]=cs_CZ.utf-8 ): must use subscript when assigning associative array
可以从文件中读取数组吗?
进行以下两项更改:删除declare语句中的括号,并使用带选项-r的read(禁用转义字符):
while read -r line; do declare -A row="$line" ... done