How do I prompt for Yes/No/Cancel input in a Linux shell script?

后端 未结 30 2328
不思量自难忘°
不思量自难忘° 2020-11-22 04:52

I want to pause input in a shell script, and prompt the user for choices.
The standard Yes, No, or Cancel type question.
How d

30条回答
  •  野的像风
    2020-11-22 05:20

    Here's something I put together:

    #!/bin/sh
    
    promptyn () {
        while true; do
            read -p "$1 " yn
            case $yn in
                [Yy]* ) return 0;;
                [Nn]* ) return 1;;
                * ) echo "Please answer yes or no.";;
            esac
        done
    }
    
    if promptyn "is the sky blue?"; then
        echo "yes"
    else
        echo "no"
    fi
    

    I'm a beginner, so take this with a grain of salt, but it seems to work.

提交回复
热议问题