You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Bash支持yes/no/cancel的交互式提示函数开发求助

Hey there! Let's get that ask function working properly for you. I see you're running into issues with set -e compatibility and the cancel behavior—let's walk through a solution that checks all your boxes.

Fixed ask Function
ask() {
    local prompt="$1"
    local default="y"  # Adjust default to "n" if needed
    while true; do
        # Show prompt with options
        read -p "$prompt [Y/n/c] " answer

        # Handle empty input with default value
        if [[ -z "$answer" ]]; then
            answer="$default"
        fi

        # Normalize input to lowercase for consistent matching
        local lower_answer=$(echo "$answer" | tr '[:upper:]' '[:lower:]')

        case "$lower_answer" in
            y|yes)
                echo "true"
                return 0
                ;;
            n|no)
                echo "false"
                return 1
                ;;
            c|cancel)
                echo "Aborting script..." >&2
                # Terminate the entire script, whether in main shell or subshell
                if [[ "$BASHPID" -eq "$$" ]]; then
                    # Running directly in main shell—exit immediately
                    exit 1
                else
                    # Running in subshell (variable assignment)—kill main script
                    kill -SIGTERM "$$"
                    exit 1
                fi
                ;;
            *)
                echo "Invalid input! Please enter y/yes, n/no, or c/cancel." >&2
                ;;
        esac
    done
}
How This Meets Your Requirements

Let's break down how this function addresses every need you listed:

  • Accepts question as parameter: Uses $1 to capture your prompt text.
  • Supports [y/n/c] and case variants: Converts input to lowercase, so Y, YES, N, Cancel all work as expected.
  • Returns correct values for yes/no: Outputs "true"/return 0 for yes (works with if statements) and "false"/return 1 for no (triggers else branches).
  • Works with variable assignment or if statements:
    • Use in if: if ask "Are you happy?"; then echo "Awesome!"; fi
    • Assign to variable: answer=$(ask "Save changes?")
  • Cancel terminates the entire script: Checks if it's running in the main shell or a subshell (from variable assignment) and ensures the whole script stops either way.
  • Retries on invalid input: Loops until a valid answer is given.
  • Empty input uses default: Falls back to the default variable (set to "y" here, easy to change).
  • Compatible with set -e and set +e:
    • In set -e mode, return 1 for no doesn't crash the script because it's either in an if condition (a "tested" command that set -e ignores) or part of a variable assignment (which has a success exit code regardless of the command inside).
    • Cancel works reliably in both modes, as it directly terminates the script.
Usage Examples

1. Using with if Statement (works with set -e)

set -e
if ask "Ready to start the deployment?"; then
    echo "Initiating deployment..."
    # Add your deployment commands here
else
    echo "Deployment cancelled by user."
fi

2. Assigning to a Variable (works with set +e)

set +e
answer=$(ask "Do you want to delete temporary files?")
if [[ "$answer" == "true" ]]; then
    rm -rf /tmp/temp_files/
    echo "Temporary files deleted."
else
    echo "Temporary files retained."
fi

3. Testing Cancel

Try entering c or Cancel in either of the above examples—your entire script will terminate immediately, as expected.

What Was Wrong With the Original Code?
  1. Cancel only worked in main shell: The original exit 1 would only terminate a subshell (when using variable assignment) instead of the whole script. Our fix checks the execution context and kills the main script if needed.
  2. set -e crashes on "no": While set -e doesn't trigger on if conditions, if you ever ran the function as a standalone command (not in if or assignment), return 1 would crash the script. Our solution maintains correct if behavior while avoiding unexpected crashes in valid usage patterns.
  3. Default value handling was unclear: The original code matched empty input but didn't have an explicit default variable, making it hard to adjust later.

内容的提问来源于stack exchange,提问作者winklerrr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 08:04:34