[英]Getopts in bash script with sub-arguments
I would like to be able to run a script like this ( with sub-arguments ): I need to be able to use short and long options我希望能够运行这样的脚本(带有子参数):我需要能够使用短选项和长选项
./myscript.sh -u myname --delete-config --delete-data
./myscript.sh -a myname..........................
./myscript.sh -h
Actually i have:其实我有:
OPTS=`getopt -o a?d:h?dd?dc? --long apps,delete:,delete-data,delete-config -n 'parse-options' -- "$@"`
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi
eval set -- "$OPTS"
while true; do
case "$1" in
-a | --apps )
showHelp
exit 0
;;
-d | --delete )
username="$2"
echo $username
shift 2
echo "$1"
echo "$2"
# Here i can add argument to the command
case "$1" in
-dd | --delete-data )
deleteData
shift 1
;;
-dc | --delete-config )
deleteConfig
shift 1
;;
* ) echo "Unexpected option: $1 - this should not happen."
showHelp
break
;;
esac
;;
-h | --help )
showHelp
break
;;
-- ) shift;
break
;;
* ) echo "Unexpected option: $1 - this should not happen."
showHelp
break
;;
esac
done
The result of the script is:脚本的结果是:
--delete toto --delete-data --delete-config --delete toto --delete-data --delete-config
toto多多
--delete-data --删除数据
--delete-config --删除配置
suppress en cours data压制过程中的数据
Unexpected option: --delete-config - this should not happen.意外选项:--delete-config - 这不应该发生。
I don't understand what i'm making wrong with shift and getopts我不明白我在 shift 和 getopts 上做错了什么
Slightly different version:版本略有不同:
#!/usr/bin/env bash
OPTS=`getopt -o a?d:h?dd?dc? --long apps,delete:,delete-data,delete-config -n 'parse-options' -- "$@"`
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi
eval set -- "$OPTS"
while test "$1" != --; do
case "$1" in
-a | --apps )
showHelp
exit 0
;;
-d | --delete )
username="$2"
echo $username
shift 2
echo "$1"
echo "$2"
# Here i can add argument to the command
while test "$1" != --; do
case "$1" in
-dd | --delete-data )
echo deleteData
shift 1
;;
-dc | --delete-config )
echo deleteConfig
shift 1
;;
* ) echo "Unexpected option: $1 - this should not happen."
showHelp
break
;;
esac
done
;;
-h | --help )
showHelp
break
;;
-- ) shift;
break
;;
* ) echo "Unexpected option: $1 - this should not happen."
showHelp
break
;;
esac
done
Not working solution, it only works in this case, not with 3 sub parameters无效的解决方案,它只在这种情况下有效,不适用于 3 个子参数
Thanks to Philippe for his comment: Let me know if you think it's not ok, or purpose better and a confirm as answer:)感谢 Philippe 的评论:如果您认为这不好,或者目的更好并确认为答案,请告诉我:)
OPTS=`getopt -o a?d:h?dd?dc? --long apps,delete:,delete-data,delete-config -n 'parse-options' -- "$@"`
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi
eval set -- "$OPTS"
while true; do
case "$1" in
-a | --apps )
showHelp
exit 0
;;
-d | --delete )
username="$2"
echo $username
shift 2
echo "$1"
echo "$2"
echo "$3"
while true; do
case "$1" in
-dd | --delete-data )
deleteData
shift
;;
-dc | --delete-config )
deleteConfig
shift
break
;;
* ) echo "Unexpected option: $1 - this should not happen."
showHelp
break
;;
esac
done
;;
-h | --help )
showHelp
break
;;
-- ) shift;
break
;;
* ) echo "Unexpected option: $1 - this should not happen."
showHelp
break
;;
esac
done
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.