所以在下面的代码中,我想尝试让我的代码读取用户在我的代码中输入的输入,如下所示
#./MyProject -a -b OR -b -a
但是,我不断收到语法错误,并且未通过程序给出的测试:以下测试是:输入不是 a&b(即 cz)、根本没有输入、参数太少、参数太多,
#Use just prints out the format like this : ./MyProject -a -b
- if ( ! getopts ":ab" arg) then
echo $use
fi
while [getopts ":ab" arg2]
do
case $arg2 in
t) if (($1 != "t" && $1 != "o")); then
echo $use
fi
esac
done
}
答案1
以下示例应该适合您。
#!/bin/bash
usage() {
echo "Usage: $0 -a -b"
exit
}
while getopts ":a:b:" arg; do
case $arg in
a)
a=${OPTARG}
(($a == "t" || $a == "o")) || usage
;;
b)
b=${OPTARG}
;;
*)
usage
;;
esac
done
echo $a
echo $b