我想讀了3個不同名稱的不同陣列類似文件的功能。因爲我不想使用不必要的代碼,我正嘗試創建可以接受數組名稱作爲參數的函數,但是我收到錯誤「未找到命令」。傳遞變量名作爲參數去在bash
hello.sh文件代碼:
#!/bin/bash
declare -a row_1
declare -a row_2
declare -a row_3
load_array()
{
ROW="$2"
let i=0
while read line; do
for word in $line; do
$ROW[$i]=$word
((++i))
done
done < $1
}
load_array $1 row_1
load_array $2 row_2
load_array $3 row_3
調用從終端這個文件有:錯誤我得到的sh hello.sh 1.txt 2.txt 3.txt
列表:
hello.sh: line 13: row_1[0]=9: command not found
hello.sh: line 13: row_1[1]=15: command not found
hello.sh: line 13: row_1[2]=13: command not found
hello.sh: line 13: row_2[0]=12: command not found
hello.sh: line 13: row_2[1]=67: command not found
hello.sh: line 13: row_2[2]=63: command not found
hello.sh: line 13: row_3[0]=75: command not found
hello.sh: line 13: row_3[1]=54: command not found
hello.sh: line 13: row_3[2]=23: command not found
BashFAQ#6是話題的這個問題的許多變種:http://mywiki.wooledge.org/BashFAQ/006 – 2014-11-21 01:33:54