在 tcshell 中传递部分参数

发布于 2024-08-31 10:20:14 字数 210 浏览 2 评论 0原文

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

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

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

I'm writing a shell script (tcsh) that is supposed to received 3 parameters or more. The first 3 are to be passed to a program, and the rest are supposed to be passed to another program. All in all the script should look something like:

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

The problem is that I don't know how to do the latter - pass all parameters that are after the third.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月光色 2024-09-07 10:20:14

我向您介绍 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

I present to you the shift command:

shift [variable]
Without arguments, discards argv[1] and shifts the members of argv to the left.

Example:

$ 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
无法言说的痛 2024-09-07 10:20:14

您可以按如下方式使用 shift

./first_program $1 $2 $3

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

./second program $* 

$* 将包含其余参数。

在执行 shift 之前,您还必须通过检查 $#argv 并确保其非零来进行错误检查。或者,您可以检查脚本开头处的 $#argv 值,并确保它至少为 3

You can make use of shift as follows:

./first_program $1 $2 $3

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

./second program $* 

$* will contain the remaining parameters.

You must also do error checking before you do a shift by checking $#argv and ensuring it is non-zero. Alternatively you can check the value of $#argv at the star of the script and ensure that it is atleast 3.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文