shell参数个数shell 参数个数
20190904
参数个数
在写shell脚本的时候,会有这样的需求:根据传⼊的参数个数来选择相应的操作。
命令$#返回出⼊的参数,但要注意对$#的引⽤范围
测试代码
#!/bin/bash
#参数个数命令 $#
#date:2018-08-08
date1=$1
date2=$2
let nump1=$#
echo "---函数外---"$nump1
function main(){
shell 字符串长度let nump2=$#
echo "---函数内---"$nump2
if [[ $nump1 = 1 ]]
then
let date2int="${date1:0:4}${date1:5:2}${date1:8:2}"
echo $date2int
exit
fi
echo $date1
}
main
1、条件判断为:if [[ $nump1 = 1 ]]
$ sh test_num_parameter.sh 2018-08-09
输出
---函数外---1
---函数内---0
20180809
2、条件判断为:if [[ $nump2 = 1 ]]
$ sh test_num_parameter.sh 2019-01-01
输出
---函数外---1
---函数内---0
2019-01-01
$#的作⽤域只在最外层
参考

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。