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
2
  • like this? Commented Dec 19, 2015 at 6:40
  • what do you mean? Commented Dec 19, 2015 at 6:55

1 Answer 1

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
  • 1
    @mikeserv I think title of question needs clarification/correction. The case option c is already provided for exit! Commented Dec 19, 2015 at 7:16

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.