これをkshで機能させることはできますが、bashでは機能させられないため、本当に気が狂います。うまくいけば、私が見落としていることは明らかです。
出力の各行が配列インデックスに格納される外部コマンドを実行する必要があります。
この単純化された例は、ループ内で配列を正しく設定しているように見えますが、ループが完了すると、それらの配列の割り当てはなくなりますか? まるでループが完全に外部シェルとして扱われているかのようです。
ジャンク.txt
this is a
test to see
if this works ok
testa.sh
#!/bin/bash
declare -i i=0
declare -a array
echo "Simple Test:"
array[0]="hello"
echo "array[0] = ${array[0]}"
echo -e "\nLoop through junk.txt:"
cat junk.txt | while read line
do
array[i]="$line"
echo "array[$i] = ${array[i]}"
let i++
done
echo -e "\nResults:"
echo " array[0] = ${array[0]}"
echo " Total in array = ${#array[*]}"
echo "The whole array:"
echo ${array[@]}
出力
Simple Test:
array[0] = hello
Loop through junk.txt:
array[0] = this is a
array[1] = test to see
array[2] = if this works ok
Results:
array[0] = hello
Total in array = 1
The whole array:
hello
そのため、ループ内で array[i] を割り当て、echo がそれを検証します。しかし、ループの後、「hello」を含む array[0] に戻り、他の要素はありません。
bash 3、4、および異なるプラットフォームで同じ結果。