处理除第一个参数以外的所有参数(在 bash 脚本中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9057387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Process all arguments except the first one (in a bash script)
提问by theta
I have a simple script where the first argument is reserved for the filename, and all other optional arguments should be passed to other parts of the script.
我有一个简单的脚本,其中第一个参数是为文件名保留的,所有其他可选参数都应该传递给脚本的其他部分。
Using Google I found this wiki, but it provided a literal example:
我使用 Google 找到了这个 wiki,但它提供了一个文字示例:
echo "${@: -1}"
I can't get anything else to work, like:
我无法让其他任何工作,例如:
echo "${@:2}"
or
或者
echo "${@:2,1}"
I get "Bad substitution" from the terminal.
我从终端收到“错误替代”。
What is the problem, and how can I process all but the first argument passed to a bash script?
有什么问题,除了传递给 bash 脚本的第一个参数之外,我如何处理所有参数?
回答by Oliver Charlesworth
Use this:
用这个:
echo "${@:2}"
The following syntax:
以下语法:
echo "${*:2}"
would work as well, but is not recommended, because as @Gordonalready explained, that using *
, it runs all of the arguments together as a single argument with spaces, while @
preserves the breaks between them (even if some of the arguments themselves contain spaces). It doesn't make the difference with echo
, but it matters for many other commands.
也可以工作,但不推荐,因为正如@Gordon已经解释的那样,使用*
,它将所有参数作为一个带空格的单个参数运行在一起,同时@
保留它们之间的中断(即使某些参数本身包含空格)。它与 没有区别echo
,但它对许多其他命令很重要。
回答by Ben Hymanson
If you want a solution that also works in /bin/sh
try
如果您想要一个也适用的解决方案,请/bin/sh
尝试
first_arg=""
shift
echo First argument: "$first_arg"
echo Remaining arguments: "$@"
shift [n]
shifts the positional parameters ntimes. A shift
sets the value of $1
to the value of $2
, the value of $2
to the value of $3
, and so on, decreasing the value of $#
by one.
shift [n]
将位置参数移动n次。Ashift
将 的值设置$1
为 的值$2
,将 的值设置$2
为 的值$3
,依此类推,将 的值减$#
一。
回答by OnlineCop
http://wiki.bash-hackers.org/scripting/posparams
http://wiki.bash-hackers.org/scripting/posparams
It explains the use of shift
(if you want to discard the first N parameters) and then implementing Mass Usage
它解释了使用shift
(如果你想丢弃前 N 个参数)然后实现 Mass Usage
回答by Pavman
Came across this looking for something else.
While the post looks fairly old, the easiest solution in bash is illustrated below (at least bash 4) using set -- "${@:#}"
where # is the starting number of the array element we want to preserve forward:
遇到这个寻找别的东西。虽然这篇文章看起来很旧,但 bash 中最简单的解决方案如下所示(至少是 bash 4),set -- "${@:#}"
其中 # 是我们要向前保留的数组元素的起始编号:
#!/bin/bash
someVar=""
someOtherVar=""
set -- "${@:3}"
input=${@}
[[ "${input[*],,}" == *"someword"* ]] && someNewVar="trigger"
echo -e "${someVar}\n${someOtherVar}\n${someNewVar}\n\n${@}"
Basically, the set -- "${@:3}"
just pops off the first two elements in the array like perl's shiftand preserves all remaining elements including the third. I suspect there's a way to pop off the last elements as well.
基本上,set -- "${@:3}"
就像 perl 的shift一样,只弹出数组中的前两个元素,并保留所有剩余元素,包括第三个元素。我怀疑还有一种方法可以弹出最后一个元素。