2012-09-27 2 views
0

location.txt에서 디렉토리 위치를 추출하여 배열에 저장하려고하는데 모양이 좋지 않습니다. 내가 스크립트를 실행하면, 배열에 텍스트 줄을 저장하는 중 오류가 발생했습니다.

난이 오류가 여기에

./test.sh: line 7: location.txt: No such file or directory [email protected] $ ./test.sh 
    ./test.sh: line 5: =/apps/IBM: No such file or directory 
    ./test.sh: line 5: =/usr/home: No such file or directory 
    ./test.sh: line 11: Unix_Array[0]: command not found 

    ./test.sh: line 11: Unix_Array[1]: command not found 

내 코드

#!/bin/sh 

    counter=0 
    while read -r line; do 
     ${Unix_Array[${counter}]}=$line; 
     let counter=counter+1; 
    done < location.txt 

    for ((i=0 ;counter > i; i++)) 
    do 
     echo $(Unix_Array[$i]) 
    done 

텍스트 파일 location.txt

/apps/IBM 
    /usr/home 
    /var/login 

이에게 할 수있는 몇 가지 하나를 얻을 내가 뭘 잘못했는지 말해 줄래? 내가 방금 할당 할 때문에

+1

AIX에서 'bash'또는 '/ bin/sh'가 무엇이든 실제로 사용 하시겠습니까? –

+0

당신의 오류는'echo $ {Unix_Array [$ i]}'대신'echo $ (Unix_Array [$ i])'에서 나온 것입니다. –

답변

1

그냥 ${Unix_Array[${counter}]}=$line;에 첫 번째 ${}을 제거 사전에

감사합니다 (I은 UNIX에서 정말 새로운 해요) ... 내가 모르는 바보 같은 실수를했을 수 있습니다 배열에, 그 값을 검색 할당하지, 당신의 고정 된 스크립트는 다음과 같이 될 것이다 :

#!/bin/sh 

counter=0 
while read -r line; do 
    Unix_Array[${counter}]=$line; 
    let counter=counter+1; 
done < location.txt 

for ((i=0 ;counter > i; i++)) 
do 
    echo ${Unix_Array[$i]} 
done 
+0

정말 고마워요! 작동합니다! : D – user1516649

+0

그리고 '#!/bin/bash'로 업데이트하십시오. 배열은 많은 배포판에서 기본'sh'에 의해 지원되지 않습니다. –

1

당신이 bash는 4를 사용하고있는 이상, 당신은 readarray 명령에 찬성 while read 루프를 포기 할 수있는 경우 :

관련 문제