2

I just create a new shell script and having trouble with small issue. Below is my script :-

function mainMenu(){
    while :
    do
    echo -e "\nMain Menu:"
    echo " A. Process Managements Utilities"
    echo " B. Memory Managements Utilities"
    echo " C. Exit"

    read -p "Select :" menuSelect
    echo

    case $menuSelect in

            a|A) processMgmt;;
            b|B) memoryMgmt;;
            c|C) exit 0;;

            *)echo "Invalid Input"
              echo
              ;;
    esac
    done
}

When user Enter 'INVALID INPUT', the function will print all mainMenu() output :-

Main Menu:
 A. Process Managements Utilities
 B. Memory Managements Utilities
 C. Exit

Select :e
Invalid Input

Main Menu:
 A. Process Managements Utilities
 B. Memory Managements Utilities
 C. Exit

 Select :

How to print only select: if user input is invalid?

Select:e
Invalid input
Select:s
Invalid input
5

As you want to print information about choice A,B etc only first time, start while loop after printing it:

function mainMenu(){

    echo -e "\nMain Menu:"
    echo " A. Process Managements Utilities"
    echo " B. Memory Managements Utilities"
    echo " C. Exit"

while :
    do

    read -p "Select :" menuSelect
    echo

    case $menuSelect in

            a|A) processMgmt;;
            b|B) memoryMgmt;;
            c|C) exit 0;;

            *)echo "Invalid Input"
              echo
              ;;
    esac
    done
}

Example output (which is expected from question):

Main Menu:
 A. Process Managements Utilities
 B. Memory Managements Utilities
 C. Exit
Select :q

Invalid Input

Select :q

Invalid Input

By means of this, information about choice selection is printed once when function mainMenu is called then while loop read the input and case do the job you want. In case of invalid input, while loop again ask by read -p "Select".

  • 1
    @mikeserv I think title of question needs clarification/correction. The case option c is already provided for exit! – Pandya Dec 19 '15 at 7:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.