提问后未能执行第一个条件

提问后未能执行第一个条件

所以我正在尝试构建我的第一个脚本,但它无法正确执行。

我想进入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

对于这种情况,我更喜欢使用 switch case:

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

相关内容