다른 배열에 다른 이름을 가진 3 개의 비슷한 파일을 읽으려고합니다. 왜냐하면 나는 불필요한 코드를 사용하고 싶지 않았기 때문에 배열 이름을 params로 받아들이는 함수를 만들려고하는데, 'command not found'오류가 발생합니다.변수 이름을 매개 변수로 전달하여 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 –