获取传递给shell脚本的最后一个参数 [英] Getting the last argument passed to a shell script

查看:682
本文介绍了获取传递给shell脚本的最后一个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ 1 是第一个参数。结果
$ @ 是所有的人。

$1 is the first argument.
$@ is all of them.

如何才能找到传递到外壳的最后一个参数
脚本?

How can I find the last argument passed to a shell script?

推荐答案

这是一个黑客位的:

for last; do true; done
echo $last

这之一,也是pretty便携式(再次,应使用bash和ksh和sh工作),它不移位的参数,这可能是好的。

This one is also pretty portable (again, should work with bash, ksh and sh) and it doesn't shift the arguments, which could be nice.

它使用的事实,隐式遍历的参数,如果你不告诉它怎么遍历,而事实上,for循环变量不作用域:他们保持他们被设置为最后一个值

It uses the fact that for implicitly loops over the arguments if you don't tell it what to loop over, and the fact that for loop variables aren't scoped: they keep the last value they were set to.

这篇关于获取传递给shell脚本的最后一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆