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

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

    As a friend of a one line command I used the following:

    while [ -z $prompt ]; do read -p "Continue (y/n)?" choice;case "$choice" in y|Y ) prompt=true; break;; n|N ) exit 0;; esac; done; prompt=;
    

    Written longform, it works like this:

    while [ -z $prompt ];
      do read -p "Continue (y/n)?" choice;
      case "$choice" in
        y|Y ) prompt=true; break;;
        n|N ) exit 0;;
      esac;
    done;
    prompt=;
    

提交回复
热议问题