深入探索Shell脚本:用户输入处理全攻略
1. 获取最后一个参数
在Shell脚本中,获取最后一个参数的操作需要注意语法。最初尝试使用如下代码时会出现问题:
echo The number of parameters is $#
echo The last parameter is ${$#}
exit
执行示例:
$ ./badlastparamtest.sh one two three four
The number of parameters is 4
The last parameter is 2648
显然,这里出现了错误。问题在于不能在花括号内使用美元符号,而应将其替换为感叹号,修改后的代码如下:
#!/bin/bash
# Testing grabbing the last parameter
#
echo The number of parameters is $#
echo The last parameter is ${!#}
exit
执行示例:
$ ./goodlastparamtest.sh one two three four
The number of parameters is 4
The last parameter is four
$ ./goodlastpara
超级会员免费看
订阅专栏 解锁全文
1869

被折叠的 条评论
为什么被折叠?



