获取shell脚本最后一个参数 [英] Get last parameter on shell script
问题描述
1的情况下:
假设我传递一些参数来我的shell脚本如下:
case 1 : suppose I am passing a number of parameters to my shell script as follows :
./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 ]:
var1 = `echo "$#"`
2的情况下:
$ 4给出了我的最后一个参数的名称。
case 2 : $4 gives me the name of last argument .
如果我希望它存储在
VAR2然后
var2 = $4
我的问题是:
如果我想存储的价值,我从VAR1以直接VAR2,怎么会是有可能的shell脚本?
If I want to store value I get from var1 to var2 directly , how would be it possible in shell script ?
为前:
./myshell_script.sh a b c
var1 = `echo "$#"` ie var1 = 3
现在我想
VAR2 = C [即总是最后一个参数,因为我不知道如何参数的用户数量众多,从驾驶室管理线传递]
var2 = c [ ie always last parameter , since I dont know how many number of parameters user is passing from comand line ]
我有什么做的?
推荐答案
下面的脚本显示了如何可以得到传递给脚本的第一个和最后一个参数:
The script below shows how you can get the first and last arguments passed to a script:
numArgs="$#"
echo "Number of args: $numArgs"
firstArg="$1"
echo "First arg: $firstArg"
lastArg="${!#}"
echo "Last arg: $lastArg"
输出:
$ ./myshell_script.sh a b c d e f
Number of args: 6
First arg: a
Last arg: f
这篇关于获取shell脚本最后一个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!