遍历所有的参数数组,不知道有多少争论,在shell脚本? [英] Loop over the arguments array, without knowing how many arguments, in a shell script?
问题描述
我想很多参数传递给shell脚本,我不知道他们有多少论点将是,我要来处理它们。我做了以下code:
I want to pass many arguments to a shell script which I don't know how many arguments they are going to be and I want to handle them. I did the following code:
int=$1
src=$2
r=$3
string=$4
duration=$5
./start.sh $int $r $src "$string"
sleep $duration
shift; shift; shift; shift; shift
while [ $# -gt 2 ]
do
r=$1
string=$2
duration=$3
./change.sh $int $r "$string"
sleep $duration
shift; shift; shift
done
这工作,但只对一次,现在我想这个脚本来运行所有的时间,我的意思是使用,而1
但不会以这种方式工作,因为参数列表就是在那个code结束时清空!
That works but only for one time, now I want this script to run all the time, I mean using while 1
but that won't work this way because the argument list is empty at the end of that code!
有没有办法做类似下面的shell脚本伪code:
Is there any way to do something like the following "pseudo code" in shell scripts:
for( i=0 ; i<arguments.count; i++ ){
//do something with arguments[i]
}
或复制参数数组到另一个数组,这样我可以在以后使用它,我想要的方式。
我的意思是我可以复制 $ *
或 $ @
或 ARG
到另一个阵列?
or copying the arguments array into another array so that I can use it later the way I want.
I mean can I copy $*
or $@
or arg
into another array?
任何帮助是非常AP preciated。
Any help is highly appreciated.
推荐答案
参数的个数存储在可与<$ C $访问参数#
, C> $#。
The number of arguments is stored in the parameter #
, which can be accessed with $#
.
在所有参数的简单循环可以写成如下:
A simple loop over all arguments can be written as follows:
for arg
do
# do something with the argument "arg"
done
这篇关于遍历所有的参数数组,不知道有多少争论,在shell脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!