bash 获取 shell 脚本的最后一个参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19678082/
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
Get last parameter on shell script
提问by user2930942
case 1 : suppose I am passing a number of parameters to my shell script as follows :
案例 1:假设我将一些参数传递给我的 shell 脚本,如下所示:
./myshell_script a b c d
and if I run echo $#
will give me number of parameters from command line I have passed and I stored it in a variable like [ since I dont know number of arguments a user is passing ]:
如果我运行echo $#
将给我从命令行传递的参数数量,并将其存储在像 [ 因为我不知道用户传递的参数数量 ] 的变量中:
var1 = `echo "$#"`
case 2 : $4 gives me the name of last argument .
情况 2:$4 给了我最后一个参数的名称。
if i want it to store in
如果我想让它存储在
var2 then
var2 然后
var2 =
My question is :
我的问题是:
If I want to store value I get from var1 to var2 directly , how would be it possible in shell script ?
如果我想存储从 var1 直接到 var2 的值,在 shell 脚本中怎么可能?
for ex :
例如:
./myshell_script.sh a b c
var1 = `echo "$#"` ie var1 = 3
now I want
现在我想要
var2 = c [ ie always last parameter , since I dont know how many number of parameters user is passing from comand line ]
var2 = c [即总是最后一个参数,因为我不知道用户从命令行传递了多少个参数]
what I have to do ?
我该怎么办?
回答by dogbane
The script below shows how you can get the first and last arguments passed to a script:
下面的脚本显示了如何获取传递给脚本的第一个和最后一个参数:
numArgs="$#"
echo "Number of args: $numArgs"
firstArg=""
echo "First arg: $firstArg"
lastArg="${!#}"
echo "Last arg: $lastArg"
Output:
输出:
$ ./myshell_script.sh a b c d e f
Number of args: 6
First arg: a
Last arg: f
回答by fedorqui 'SO stop harming'
For this, you can use:
为此,您可以使用:
${@: -1}
Test
测试
$ cat a
#!/bin/bash
echo "passed $# parameters, last being --> ${@: -1}"
$ ./a a b c d
passed 4 parameters, last being --> d
$ ./a a b c d e f g
passed 7 parameters, last being --> g
回答by devnull
Quoting a way from here:
从这里引用一种方法:
for last; do : ; done
echo "${last}"
The last argument passed to the script would be stored in the variable last
.
传递给脚本的最后一个参数将存储在变量中last
。
As mentioned in the link, this would work in POSIX-compatible shells it works for ANY number of arguments.
如链接中所述,这适用于 POSIX 兼容的 shell,它适用于任何数量的参数。
BTW, I doubt if your script works the way you've written in your question:
顺便说一句,我怀疑你的脚本是否像你在问题中写的那样工作:
var1 = `echo "$#"`
You need to remove those spaces around =
, i.e. say:
您需要删除 周围的那些空格=
,即说:
var1=`echo "$#"`
or
或者
var1=$(echo "$#")
回答by chepner
It might be better to reorganize your parameters. Instead of a variable number of arguments followed by a specific final argument, put the last argument first (so that it is $1
), and put the variable number of arguments after that. So instead of
重新组织您的参数可能会更好。代替可变数量的参数后跟特定的最终参数,将最后一个参数放在最前面(这样它是$1
),然后将可变数量的参数放在后面。所以代替
myshell_script a b c d
with var2
eventually being set to "d", use
与var2
最终被设置为“d”,使用
var2=; shift
and call like
并打电话给
myshell_script d a b c
Now var2
will have the value the value of "d", and $@
(after the shift) will contain "a", "b", and "c".
现在var2
将具有值“d”的值,并且$@
(在移位之后)将包含“a”、“b”和“c”。