0

I have defined a function as below.

no input- exit
input 'n'-> exit
input 'y'-> continue to next step
input other than 'y' or 'n' -> retry for 3 times & still wrong input-> exit

I am not able to achieve the last retry part. can someone help?

my_func()
  {
   read -p "Still want to continue (y/n)? : " userin
   userin=${userin,,}
   if [ -z "$userin" ];then
        echo "No input"
        exit 1
   elif [ $userin == "n" ]; then
        exit 1
   elif [ $userin == "y" ]; then
        echo "Next step starting
        return
   else
        echo "You entered $userin - give proper input(y/n)"

   fi
}
dbNovice
  • 399
  • 2
  • 4
  • 18
  • Does this answer your question? [How do I prompt for Yes/No/Cancel input in a Linux shell script?](https://stackoverflow.com/questions/226703/how-do-i-prompt-for-yes-no-cancel-input-in-a-linux-shell-script) – Léa Gris Mar 31 '21 at 06:59

1 Answers1

2

Would you try the following:

my_func() {
     for ((i = 0; i < 3; i++)) {
         read -p "Still want to continue (y/n)? : " userin
         if [[ -z $userin ]]; then
             echo "No input"
             exit 1
         elif [[ $userin = "n" ]]; then
             exit 1
         elif [[ $userin = "y" ]]; then
             echo "Next step starting"
             return
         else
             echo "You entered $userin - give proper input(y/n)"
         fi
    }
    exit 1
}
tshiono
  • 21,248
  • 2
  • 14
  • 22