【发布时间】:2015-05-22 03:35:31
【问题描述】:
所以,我已经学会了如何在我的 bash 脚本中传递参数。对我来说,下一步是传递一个带有参数的标志。例如,-id 12345、-d YES 或 -d NO、-h localhost。
所以,我希望像这样执行:
./Build.sh -id 12345 -d YES -h localhost
目前,我正在这样做:
./Build.sh 12345 YES localhost
getopt,getopts 是我所追求的吗?如果是这样,你能告诉我如何在我的脚本中使用它吗?
到目前为止我的工作脚本:
if [[ $# != 3 ]]; then
echo "Usage: ./Build.sh <CUSTID> <YES|NO> <HOST>" 2>&1
exit 1
fi
# Checks for YES or NO option for Create.sql script
case $2 in
YES|NO)
filename="Build_$2.sql"
;;
*)echo "Must enter YES or NO"
exit 1
;;
esac
【问题讨论】:
-
-id不是getopts中的有效开关,它等价于-i -d。长选项需要双破折号:所以,--id就可以了。 -
好的,我很好。让我们用
-i保持简单 -
你一定要看Using
getoptsin bash shell script to get long and short command line options。我不确定是否有理由不将其作为副本关闭。