我正在尝试将 3 个具有不同名称的类似文件读取到不同的数组。因为我不想使用不必要的代码,所以我试图创建可以接受数组名称作为参数的函数,但是我收到错误“找不到命令”。
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