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



如果有零或大于2个输入,将向用户显示一条消息。

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

下面是一个示例代码,如果没有参数,则将返回"无参数":

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

参数由bash带有多个选项的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

相关内容

最新更新