使用getopt(1)解析bash中的一些选项,当我从cmd行传递选项时,它不会进行评估。它默认为 - )“双破折号”case语句并在我提供时传递其他选项,不知道为什么它会导致这种奇怪的行为,代码如下:
parse_args() {
cmd="" # Default none
isparsed=0 # If args parsed = 1
# Check the number of arguments. If none are passed, print help and exit
options=$(getopt -o hs:d:c:p:i: -l help,source:,destination:,connection:,platform:,install: -n "cdr" -- "$@")
# If the number of arguments after processing them using getopt is larger
# than zero, then it means there is an unknown argument.
if [[ $? -ne 0 ]]; then
HELP
exit 1
fi
printdbg "parsing options"
eval set -- "$options"
while true; do
case "$1" in
-h) # Show help option menu
HELP
exit 1
;;
-s|--source) # Parse options to source sub command
cmd=$2 # Remove 'source' from the argument list
shift;
if [[ $cmd == "sqlite" ]]; then
SOURCE="sqlite"
elif [[ $cmd == "mysql" ]]; then
SOURCE="mysql"
else
HELP
exit 1
fi
isparsed=1
;;
-d|--destination) # Parse options to destination sub command
cmd=$2 # Remove 'destination' from the argument list
shift;
if [[ $cmd == "postgre" ]]; then
DESTINATION="postgre"
elif [[ $cmd == "riak" ]]; then
DESTINATION="riak"
elif [[ $cmd == "both" ]]; then
DESTINATION="both"
else
HELP
exit 1
fi
isparsed=1
;;
-c|--connection) # Parse options to connection sub command
cmd=$2 # Remove 'connection' from the argument list
shift; printdbg "$cmd"
if [[ ! -z $cmd ]]; then
SOURCE_CONN=$(echo "$cmd" | awk -F "::" '{print $1}')
DESTINATION_CONN=$(echo "$cmd" | awk -F "::" '{print $2}')
parse_csv "$SOURCE_CONN" #stored in PARSED_ARR
echo ${PARSED_ARR[@]}
# ${DESTINATION_CONN:=${PARSED_ARR}}
else
HELP
exit 1
fi
isparsed=1
;;
-p|--platform) # Parse options to platform sub command
cmd=$2 # Remove 'platform' from the argument list
shift;
if [[ $cmd == "csv" ]]; then
CDR_TYPE=1
elif [[ $cmd == "api" ]]; then
CDR_TYPE=2
elif [[ $cmd == "freeswitch" ]]; then
CDR_TYPE=3
elif [[ $cmd == "asterisk" ]]; then
CDR_TYPE=4
elif [[ $cmd == "yate" ]]; then
CDR_TYPE=5
elif [[ $cmd == "kamailio" ]]; then
CDR_TYPE=6
elif [[ $cmd == "opensips" ]]; then
CDR_TYPE=7
elif [[ $cmd == "sipwise" ]]; then
CDR_TYPE=8
elif [[ $cmd == "veraz" ]]; then
CDR_TYPE=9
else
HELP
exit 1
fi
isparsed=1
;;
-i|--install) # Parse options to install sub command
cmd=$2 # Remove 'install' from the argument list
shift;
if [[ $cmd == "sqlite" ]]; then
install_dependencies
install_sqlite
elif [[ $cmd == "mysql" ]]; then
install_dependencies
install_mysql
elif [[ $cmd == "postgre" ]]; then
install_dependencies
install_postgre
elif [[ $cmd == "riak" ]]; then
printwarn "This feature will be supported in future versions"
exit 1
elif [[ $cmd == "pusher" ]]; then
install_dependencies
install_golang
install_cdrpusher
install_supervisord
elif [[ $cmd == "stats" ]]; then
install_dependencies
install_golang
install_cdrstats
else
HELP
exit 1
fi
isparsed=1
;;
--)
shift
break
;;
esac
done
}
我可以告诉它在case语句中的默认值是因为isparsed标志仍未设置(== 0)并且我从main()
将错误打印到控制台main() {
. cdr_funcs
check_permissions
detect_os
parse_args
if [[ ${isparsed} == 1 ]]; then
INSTALL_COMPLETE_MESG
exit 0
fi
printerr "isparsed flag == 0"
HELP
exit 1
}
使用-h标志运行会明显地显示:
devbox cdr # ./cdr -h
[DEBUG] [check_permissions]: Root Permissions Validated
[DEBUG] [detect_os]: Starting OS Platform detection
[DEBUG] [detect_os]: Checking OS Compatibility
[DEBUG] [detect_os]: OS detected: [linux] (OS is supported)
[DEBUG] [detect_os]: Finished OS detection
[DEBUG] [parse_args]: parsing options
[ERROR] [main]: isparsed flag == 0
答案 0 :(得分:1)
在函数(val polygon = new javafx.scene.shape.Polygon()
origPoints.foreach{case (x,y) => polygon.getPoints.addAll(x,y)}
setShape(polygon)
setStyle("-fx-border-color: red")
)内,parse_args
扩展为函数调用的参数,而不是封闭脚本。没有任何,所以"$@"
看不到任何参数,也不会产生任何选项。
如果要使用函数分析脚本参数,则需要将它们提供给函数:
getopt