sh 问是或否

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sh 问是或否相关的知识,希望对你有一定的参考价值。

# This is a general-purpose function to ask Yes/No questions in Bash, either
# with or without a default answer. It keeps repeating the question until it
# gets a valid answer.

ask() {
    # https://gist.github.com/davejamesmiller/1965569
    local prompt default reply

    if [ "${2:-}" = "Y" ]; then
        prompt="Y/n"
        default=Y
    elif [ "${2:-}" = "N" ]; then
        prompt="y/N"
        default=N
    else
        prompt="y/n"
        default=
    fi

    while true; do

        # Ask the question (not using "read -p" as it uses stderr not stdout)
        echo -n "$1 [$prompt] "

        # Read the answer (use /dev/tty in case stdin is redirected from somewhere else)
        read reply </dev/tty

        # Default?
        if [ -z "$reply" ]; then
            reply=$default
        fi

        # Check if the reply is valid
        case "$reply" in
            Y*|y*) return 0 ;;
            N*|n*) return 1 ;;
        esac

    done
}

以上是关于sh 问是或否的主要内容,如果未能解决你的问题,请参考以下文章

尾部斜线:是或否? [复制]

C++ 抽象类:构造函数是或否?

如何将计数器设置为是或否无线电,提交时递增?

MVC 模型布尔显示是或否

是或否:MVC 中的模型是不是应该包含应用程序逻辑?

如果嵌套查询在 SQL Server 中是不是有结果,如何返回是或否?