【发布时间】:2017-09-19 16:16:10
【问题描述】:
假设一个 shell 函数 my_function 期望接收三个有效的输入参数:
my_function()
{
echo "Three common metasyntactic variables are: $1 $2 $3"
}
我想在my_function 中包含一个测试,评估函数是否确实收到了三个输入参数并且这些输入参数都不是空的。
$ my_function foo bar baz
Three common metasyntactic variables are: foo bar baz
$ my_function foo bar # By default, no error message is given, which I wish to avoid
Three common metasyntactic variables are: foo bar
我将如何实现它?
编辑 1: 如上所述,我正在寻找的代码不仅可以确认输入变量的数量,而且还可以确认它们都不是空的。这第二个方面是相关的,因为输入变量可能是从其他函数传递的变量本身。
【问题讨论】:
-
使用
$#检查脚本/函数的参数数量。
标签: bash function shell if-statement arguments