要求後の最初の条件の実行に失敗しました。

要求後の最初の条件の実行に失敗しました。

そのため、最初のスクリプトを作成しようとしていますが、正しく実行されません。

スクリプト内部に入りたいのですが、git fetch --prune originその前に質問したいです。 「続行」しますか、それとも「終了」しますか? 「終了」部分は機能しますが、「続行」部分は機能しません。

#!/usr/bin/env bash

echo "Ready to git-some and sync your local branches to the remote counterparts ?"

REPLY= read -r -p 'Continue? (type "c" to continue), or Exit? (type "e" to exit): '

if [[ "${REPLY}" == 'c ' ]]
then
  echo "About to fetch"
  git fetch --prune origin
elif [[ "${REPLY}" == 'e' ]]
then
  echo "Stopping the script"
fi

ベストアンサー1

最初のif条件にスペースがあります'c '

if [[ "${REPLY}" == 'c ' ]]

条件検索またはc[space]e

それを削除します。

if [[ "${REPLY}" == 'c' ]]

elseデバッグ条件は次のとおりです。

if [[ "${REPLY}" == 'c' ]]
then
    echo "About to fetch"
    git fetch --prune origin
elif [[ "${REPLY}" == 'e' ]]
then
    echo "Stopping the script"
else
    echo "${REPLY} is INVALID"
fi

この場合、スイッチケースを使用することをお勧めします。

echo "Ready to git-some and sync your local branches to the remote counterparts ?"

read -r -p 'Continue? (type "c" to continue), or Exit? (type "e" to exit): ' REPLY

case $REPLY in
    [Cc])
        echo "About to fetch"
        git fetch --prune origin
        ;;
    [Ee])
        echo "Stopping the script"
        exit 1;;
    *)
        echo "Invalid input"
        ;;
esac

おすすめ記事