Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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
share|improve this question
    
like this? – mikeserv yesterday
    
what do you mean? – Asif yesterday
up vote 4 down vote accepted

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".

Hope this helps

share|improve this answer
1  
@mikeserv I think title of question needs clarification/correction. The case option c is already provided for exit! – Pandya yesterday
1  
thank you very much – Asif yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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