20190904
在写shell脚本的时候,会有这样的需求:根据传入的参数个数来选择相应的操作。
$#
返回出入的参数,但要注意对$#
的引用范围#!/bin/bash
#参数个数命令 $#
#date:2018-08-08
date1=$1
date2=$2
let nump1=$#
echo "---函数外---"$nump1
function main(){
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
if [[ $nump1 = 1 ]]
$ sh test_num_parameter.sh 2018-08-09
---函数外---1
---函数内---0
20180809
if [[ $nump2 = 1 ]]
$ sh test_num_parameter.sh 2019-01-01
---函数外---1
---函数内---0
2019-01-01
$#
的作用域只在最外层
原文:https://www.cnblogs.com/damahuhu/p/11675498.html