Search code examples
bashnew-operator

returning to menu in bash


i want to return to my starting point of program instead of exiting it with Break command ? for example if i run it i select 1 to add a record i make that record and than when i press q i am brought back to the same menu with

  • "1 - add a record"
  • "2 - remove record"
  • "3 - search records"
  • "q - to quit"

instead of just exiting the program

   #!/bin/bash

addtoRecord()
{
    echo
    while true
    do
        echo "to add a record to your address book, enter the info in this format:"
        echo "name,last name,@mail.com,123456789"
        echo "to quit enter 'q'."
        read aInput
        if [ "$aInput" == 'q' ]
            then
            break
        fi
        echo
        echo $aInput >> addressbook.txt
        echo
    done
}

{

    echo "1 - add a record"
    echo "2 - remove record"
    echo "3 - search records"
    echo "q - to quit"
    read input

    case $input in
        1) addtoRecord;;
        2) removeRecord;;
        3) searchRecord;;
        q) exit 
    esac

}

Solution

  • A simple while loop should do this:

    while : ; do
        echo "1 - add a record"
        echo "2 - remove record"
        echo "3 - search records"
        echo "q - to quit"
        read input
    
        case $input in
            1) addtoRecord;;
            2) removeRecord;;
            3) searchRecord;;
            q) exit 
        esac
    done