Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I was trying to trap CtrlC or CtrlZ and clear the screen and repeat the loop without exiting – it's working but I need to press Enter to repeat the loop.

#!/bin/bash
trap 'clear' 2 20
while :
do
        clear
        echo -e "\n\t1. Create a user"
        echo -e "\t2. Create a Group"
        echo -e "\t3. Delete user"
        echo -e "\t4. Reset Password"
        echo -e "\n\tSelect one of the option : \c"
        read OPT
done

When pressing CtrlC or CtrlZ it clears the screen but does not display the menu again.

I want to use this as a logon script where a user logs in and only gets this menu to work. If the user enters CtrlC or CtrlZ the script has to redisplay the menu, without coming to shell.

share|improve this question

1 Answer 1

up vote 1 down vote accepted
#!/bin/bash

trap 'menu' 2 20

menu()
{
   clear
   echo -e "\n\t1. Create a user"
   echo -e "\t2. Create a Group"
   echo -e "\t3. Delete user"
   echo -e "\t4. Reset Password"
   echo -e "\n\tSelect one of the option : \c"
}

while :; do
   menu
   read OPT
done

If you don't want to have to press the Enter key to repeat the loop, change the read command to:

read -n 1 OPT
share|improve this answer

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.