2014-12-27 3 views
1

나는이 개 파일이 이 :bash는 스크립트 간단한 배열

john smith 
adam moore 
max caviar 

내가 한 이미 한 것을 : 좀 봐 alik을했습니다

first=() 
getfirst() { 
    i=0 
    while read line # Read a line 
    do 
     array[i]=$line # Put it into the array 
     i=$(($i + 1)) 
    done < $1 
} 
getfirst "/tmp/first.txt" 
for a in "${array[@]}" 
do 
     echo "$a" 
done 

last=() 
getlast() { 
    i=0 
    while read line # Read a line 
    do 
     array[i]=$line # Put it into the array 
     i=$(($i + 1)) 
    done < $1 
} 
getlast "/tmp/first.txt" 
for b in "${array[@]}" 
do 
     echo "$b" 
done 

전자 (사용하여 반복) :

for x in {1..2} 
do 
echo $a[$x] $b[$x]; 
done 

하지만 출력은 다음과 같습니다 당신이 paste이없는 경우,

$ paste -d ' ' first.txt last.txt 
john smith 
adam moore 
max caviar 

사용 :

max caviar 
+0

'first'와'last' 배열을 만들지 만 절대로 사용하지 마십시오. 그런 다음 배열'a'와'b'에 접근합니다. 그러나 이것들은 배열로 선언되지 않았고 두 개의 for 루프에있는 반복 변수 일뿐입니다. – Barmar

답변

6

간단한 해결책이 paste을 사용하고 있습니다 어레이 :

$ first=($(cat first.txt)) 
$ last=($(cat last.txt)) 
$ for ((i = 0; i < 3; ++i)); do echo ${first[$i]} ${last[$i]}; done 
john smith 
adam moore 
max caviar 
+0

잘했다 ..... 죤 스미스 아담 moore 최대 캐비아 – moengoet

+0

correct ... thanks – moengoet