2012-12-13 22 views
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实际上有其真正的价值第四元素。

这对你有什么意义吗?

+0

在什么shell?一般来说,shell不支持数组,所以你必须谈论一个特定的shell。 –

+0

确保您引用传递给脚本的参数以保护那些带有空格的参数。同样,当你创建数组:'nwords =(“$ 1”“$ 2”“$ 3”“$ 4”)''或者更简单的'nwords =(“$ @”)' –

回答

3

数组索引从0开始,因此您需要使用${nwords[3]}来获取数组的第四个元素。

+0

true“非常感谢! – user1835630

3

数组索引从0开始,而不是1)

即:

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 
+0

true,thank you !:) – user1835630