需要一个shell脚本从命令行中获取2个数字,但它不应接受零个或多个2个参数

时间:2017-11-24 18:17:14

标签: linux shell unix

如果有0个或2个以上的输入,则会向用户显示一条消息。

我不太了解如何检查,从命令行传递了多少值。我可以获得一个简单的程序吗?

1 个答案:

答案 0 :(得分:0)

下面是一个示例代码,如果没有传递参数,将返回“无参数传递”:

#!/bin/bash
if [ $# = 0 ];
then
echo "No argument is passed"
fi

参数由具有多个选项的bash处理,下面供您参考:

$#  is the number of parameters passed
$0  returns the name of the shell script running as well as its    location in the file system
$*  gives a single word containing all the parameters passed   to the script 
$@  gives an array of words containing all the parameters  passed to the script