getopts を使用しようとしている bash 関数があり、問題が発生しています。
この関数は、それ自体 ( getch
)、オプションの-s
フラグ ( getch -s
)、またはオプションの文字列引数を後で呼び出すように設計されています (したがってgetch master
、 とgetch -s master
は両方とも有効です)。
以下のスニペットは私の問題がある場所です - それは機能全体ではありませんが、私が焦点を当てているものです:
getch()
{
if [ "$#" -gt 2 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
echo "Usage: $0 [-s] [branch-name]" >&2
return 1
fi
while getopts "s" opt; do
echo $opt # This line is here to test how many times we go through the loop
case $opt in
s)
squash=true
shift
;;
*)
;;
esac
done
}
getch -s master
奇妙なことが起こるのはケースです。上記はs
一度吐き出されるはずですが、代わりに次のようになります。
[user@host:git-repositories/temp]$ getch -s master
s
s
[user@host:git-repositories/temp]$
-s
opt を 2 回 解析するのはなぜですか?