In a set of if/elif/else/fi statements, I have made 'else' present the user with an error message, but I also want it to take the user back to the question which was asked before the if/else statements so that they can try to answer it again.How do I take the user back to a previous line of code? Or, if this is not possible, is there another way to do this?
How to repeat prompt to user in a shell script?
linux;bash;shell script
I think the easiest way would be to wrap the prompting code into a function, and then drop it into an until loop.Since all you need really is to call the function until it succeeds, you can put the noop command : in the until loop.Something like this:#!/bin/bashgetgender() {  read -p What is the gender of the user? (male/female):  gender  case $gender in    m|M)      grouptoaddto=boys      return 0      ;;    f|F)      grouptoaddto=girls      return 0      ;;    *)      printf %s\\n Please enter 'M' or 'F'      return 1      ;;  esac}until getgender; do : ; donesudo usermod -a -G $grouptoaddto $usernameThe point here is the function called with until, so it is repeatedly called until it succeeds.  The case switch within the function is just an example.Simpler example, without using a function:while [ -z $groupname ]; do  read -p What gender is the user? answer  case $answer in    [MmBb]|[Mm]ale|[Bb]oy) groupname=boys ;;    [FfGg]|[Ff]emale|[Gg]irl) groupname=girls ;;    *) echo Please choose male/female (or boy/girl) ;;  esacdonesudo usermod -a -G $groupname $usernameIn this last example, I'm using the -z switch to the [ (test) command, to continue the loop as long as the groupname variable has zero length.The keynote is the use of while or until.To translate this last example into human readable pseudocode:While groupname is empty,  ask user for gender.  If he answers with one letter m or B,    or the word Male or boy,    set the groupname as boys.  If she answers with one letter F or g,    or the word female or Girl,    set the groupname as girls.  If he/she answers anything else, complain.(And then repeat, since groupname is still empty.)Once you have groupname populated,  add the user to that group.Yet another example, without the groupname variable:while true; do  read -p What gender is the user? answer  case $answer in    [MmBb]|[Mm]ale|[Bb]oy)      sudo usermod -a -G boys $username      break      ;;    [FfGg]|[Ff]emale|[Gg]irl)      sudo usermod -a -G girls $username      break      ;;    *) echo Please choose male/female (or boy/girl) ;;  esacdone