【发布时间】:2011-04-04 21:01:31
【问题描述】:
我明白了
foo() {
if [[ $# -lt 1 ]]; then
return 0
fi
...
}
使用 $# 进行比较的具体内容是什么?
【问题讨论】:
标签: bash scripting shell syntax
我明白了
foo() {
if [[ $# -lt 1 ]]; then
return 0
fi
...
}
使用 $# 进行比较的具体内容是什么?
【问题讨论】:
标签: bash scripting shell syntax
$# 是传递给脚本的参数数量。有关完整列表,请参阅 bash(1) 手册页的 PARAMETERS 部分的 Special Parameters 小节。
【讨论】:
$# = 传递给函数的参数数量。
在您的代码中,如果未使用至少一个参数调用该函数,则该函数将返回 0。
【讨论】:
$# 表示传递给脚本的命令行参数的数量。
sh-3.2$ cat a.sh
echo $# #print the number of cmd line args.
sh-3.2$ ./a.sh
0
sh-3.2$ ./a.sh foo
1
sh-3.2$ ./a.sh foo bar
2
sh-3.2$ ./a.sh foo bar baz
3
在函数内部使用时(如您的情况),它表示传递给函数的参数数量:
sh-3.2$ cat a.sh
foo() {
echo $# #print the number of arguments passed to the function.
}
foo 1
foo 1 2
foo 1 2 3
sh-3.2$ ./a.sh
1
2
3
【讨论】: