如果您想支持长期选项,您应该使用外部getopt
实用程序。如果你只需要支持短期选项,最好使用Bash内建的getopts
。
下面是使用getopts
的例子(getopt
没有太多不同):
options=':q:nd:h'
while getopts $options option
do
case $option in
q ) queue=$OPTARG;;
n ) execute=$FALSE; ret=$DRYRUN;; # do dry run
d ) setdate=$OPTARG; echo "Not yet implemented.";;
h ) error $EXIT $DRYRUN;;
\?) if (((err & ERROPTS) != ERROPTS))
then
error $NOEXIT $ERROPTS "Unknown option."
fi;;
* ) error $NOEXIT $ERROARG "Missing option argument.";;
esac
done
shift $(($OPTIND - 1))
不是你的第一个测试总是显示true
结果,并会在当前创建一个名为“1”的文件目录。您应该使用(按优先顺序排列):
if (($# > 1))
或
if [[ $# -gt 1 ]]
或
if [ $# -gt 1 ]
而且,对于一个任务,你不能有等号周围的空间:
foo=1
@Prospero:断开链接 – realtebo 2018-01-28 08:56:37