0

我正在编写一个应该接收 3 个或更多参数的 shell 脚本(tcsh)。前 3 个要传递给一个程序,其余的应该传递给另一个程序。总而言之,脚本应该类似于:

./first_program $1 $2 $3
./second program [fourth or more]

问题是我不知道如何做后者 - 传递第三个之后的所有参数。

4

2 回答 2

2

我向您展示shift命令:

shift[变量]
不带参数,丢弃 argv[1] 成员argv并将其向左移动。

例子:

$ cat /tmp/tcsh.sh
#!/bin/tcsh

echo "$1" "$2" "$3"
shift
shift
shift
echo "$*"
$ /tmp/tcsh.sh 1 2 3 4 5 6
1 2 3
4 5 6
于 2010-05-08T14:24:49.050 回答
1

您可以使用shift如下:

./first_program $1 $2 $3

shift # shift 3 times to remove the first 3 parameters.
shift
shift

./second program $* 

$*将包含其余参数。

您还必须在执行 a 之前进行错误检查,方法shift是检查$#argv并确保它不为零。或者,您可以检查$#argv脚本星号处的值并确保它至少为3

于 2010-05-08T14:25:15.723 回答