Arrays 我可以使用多少个参数
我有一个问题:跑步的时候Arrays 我可以使用多少个参数,arrays,shell,parameters,Arrays,Shell,Parameters,我有一个问题:跑步的时候 ./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开始,因此需要使
./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开始,因此需要使用${nwords[3]}
获取数组的第四个元素。数组索引从0开始,而不是从1;)
i、 e:
在什么壳里?通常,shell不支持数组,因此您必须谈论特定的shell。请确保引用传递给脚本的参数,以保护带有空格的参数。类似地,当您创建数组时:nwords=(“$1”“$2”“$3”“$4”)
或更简单的nwords=(“$@”)
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