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

后端 未结 30 1720
不思量自难忘°
不思量自难忘° 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:14

    inquire ()  {
      echo  -n "$1 [y/n]? "
      read answer
      finish="-1"
      while [ "$finish" = '-1' ]
      do
        finish="1"
        if [ "$answer" = '' ];
        then
          answer=""
        else
          case $answer in
            y | Y | yes | YES ) answer="y";;
            n | N | no | NO ) answer="n";;
            *) finish="-1";
               echo -n 'Invalid response -- please reenter:';
               read answer;;
           esac
        fi
      done
    }
    
    ... other stuff
    
    inquire "Install now?"
    
    ...
    

提交回复
热议问题