以bash返回菜单

时间:2018-11-03 12:23:21

标签: bash new-operator

我想返回到程序的起点而不是使用Break命令退出吗?例如,如果我运行它,那么我选择1来添加一条记录,我将其记录下来,然后当我按q时,我将使用

返回同一菜单
  • “ 1-添加记录”
  • “ 2-删除记录”
  • “ 3-搜索记录”
  • “ q-退出”

不仅仅是退出程序

   #!/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

}

1 个答案:

答案 0 :(得分:0)

一个简单的while循环应该做到这一点:

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