在bash shell中解析命令行选项的最佳方法是什么? [英] What is the best way to parse command line options in bash shell?
本文介绍了在bash shell中解析命令行选项的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,我有以下脚本对我从这里:
So I have the following script to sort command line options I got it from here:
optstring=h
unset options #deletes options
while(($#));
do
echo $item;
case $1 in
-[!-]?*) # caso a opção seja do tipo -ab
for ((i=1; i < ${#1}; i++));do # Loop sobre cada caracter
c=${1:i:1}
options+=("-$c")
if [[ $optstring = *"$c:"* && ${1:i+1} ]];
then
options+=("${1:i+1}")
break
fi
done;;
#type --foo=bar
--*[^=*]) options+=("${1%%=*}") ;
echo "${options[@]}";;
#THIS ONE IS NOT WORKING IDK WHY
--?*=*) options+=("${1%%=*}" "${1#*=}") ;
echo "${options[@]}";;
# adds --endopts for --
--) options+=(--endopts)
echo "${options[@]}";;
*) options+=("$1")
echo "${options[@]}";;
esac
shift
done
除了不能正常工作外,我认为还有更好的方法可以实现.
谁能指出我正确的方向,或者至少告诉我我做错了什么?
And besides not working properly I feel that there is a better way to do this.
Can anyone point me in the right direction or at least tell me what I am doing wrong?
推荐答案
使用内置的shell getopts
或GNU命令 getopt
.
Use shell built-in getopts
or GNU command getopt
.
这篇关于在bash shell中解析命令行选项的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文