2

私はこの問題を抱えています:実行中

./choose_words.sh $NWORDS_s1 $NWORDS_s2 $NWORDS_s3 $NWORDS_s4

Choose_wordsで、実行した後nwords=($1 $2 $3 $4)$4値が含まれていないようです。したがって、印刷しようとすると:

echo ${nwords[4]} # I get nothing from this

一方、印刷しようとするとecho ${nwords[*]}、配列nwordsには実際には実際の値を持つ4番目の要素があります。

これはあなたにとって意味がありますか?

4

2 に答える 2

3

配列のインデックスは から始まるため、 を使用して配列の 4 番目の要素を取得する0必要があります。${nwords[3]}

于 2012-12-13T17:51:28.403 に答える
3

配列のインデックスは 1 ではなく 0 から始まります ;)

すなわち:

echo ${nwords[0]} # This is the 1st element, corresponding to $1
echo ${nwords[1]} # This is the 2nd element, corresponding to $2
echo ${nwords[2]} # This is the 3rd element, corresponding to $3
echo ${nwords[3]} # This is the 4th element, corresponding to $4
于 2012-12-13T17:51:29.043 に答える