2014-04-10 4 views
1

나는 다음과 같은 간단한 코드에서 오류가 발생하는 이유를 알아 내기 위해 많은 시간을 보냈습니다. 누구든지 해결할 수 있으면 고맙습니다.쉘 스크립트에서 문자열의 배열에 값을 할당하는 방법은 무엇입니까?

i=0 
while read line 
do 
    if [[ -z "$line" ]]; then 
     echo "End of numbers" 
     break 
    else 
    { 
     echo "$line is not empty" 
     array[$i] = $line 
     echo array[$i] 
     ((i += 1)) 

    } 
    fi 
done 

출력 :

sss 
sss is not empty 
command.sh: line 10: array[0]: command not found 
array[0] 
ss2 
ss2 is not empty 
command.sh: line 10: array[1]: command not found 
array[1] 

답변

3

대신 :

array[$i]="$line" 

또는보다 효율적으로 사용하는 구문을 추가 할 :

array[$i] = $line 

당신은 BASH에서 =에 공백을 제거해야 아라에있는 요소 y :

array+=("$line") 
관련 문제