Bash: カスタマイズ機能の改善に役立つ

Bash: カスタマイズ機能の改善に役立つ

私はBashを学んでいて、基本的な機能を書いています。

wsgc () {
    # Wipe the global variable value for `getopts`.
    OPTIND=1;
    echo "git add -A";
    while getopts m:p option;
    do
        case "${option}"
        in
            m)
                COMMIT_MESSAGE=$OPTARG
                if [ "$COMMIT_MESSAGE" ]; then
                    echo "git commit -m \"$COMMIT_MESSAGE.\""
                else
                    echo "A commit message is required."
                    exit
                fi
                ;;
            p)
                echo "git push"
                exit
                ;;
            \?)
                echo "Invalid parameter."
                exit
                ;;
        esac
    done
}

しかし、いくつかの問題で苦労しています。

  1. inは動作しません。引数を省略すると、ifBashがm)介入してセッションから私を追い出すためです。

    git add -A
    -bash: option requires an argument -- m
    Invalid parameter.
    logout
    Saving session...
    ...copying shared history...
    ...saving history...truncating history files...
    ...completed.
    

    [処理完了]

  2. 次を実行した後、wsgc -m "Yo!" -pセッションから追い出されました。

    git add -A
    git commit -m "Yo."
    git push
    logout
    Saving session...
    ...copying shared history...
    ...saving history...truncating history files...
    ...completed.
    

    [処理完了]

どんなアドバイスも本当にありがとうございます。

ベストアンサー1

m)のifが動作しません。引数を省略すると、Bashが介入してセッションから私を追い出すためです。

後に指定するgetopts m:p optionことは、引数が必要であることを意味します。指定しないとエラーが発生します。:m

実行後:wsgc -m "Yo! -p、セッションから追い出されました。

セッションから追い出されるとはどういう意味ですか?殻が消えたか?その理由は、スクリプトを実行せずにインポートしたからです。

つまり、をgetopt使用することをお勧めしますgetopts

おすすめ記事