Bash脚本菜单循环

时间:2017-09-28 17:02:12

标签: linux bash loops menu

我已经创建了一个菜单功能,并且能够输入并在输入时调用该功能。我只是想知道如何在完成一个功能之后再次调用菜单。

我在这里有一个我正在寻找的例子。我有一个菜单和它的功能。我希望能够在使用其中一个功能后循环回菜单。这可能是一个简单的修复,但我看了很多地方,找不到我要找的东西。

#!/bin/sh
function Main_Menu
clear
echo "Select an option ..."
printf "\n"
echo "1 - Check Free Disk Space"
echo "2 - List Directories"
echo "3 - Show Running Processes"
echo "4 - TCP Connections"
echo "5 - Network Interfaces"
echo "6 - Show PATH"
echo "7 - Show Computer Hostname"
echo "8 - Routing Table"
echo "9 - Computer Uptime"
echo "10 - Available Block Devices"
echo "11 - Mount Device"
echo -n "Input desired function number and press ENTER: "
read user_input    
if [ "$user_input" = "1" ]
then
    Check_Disk
elif [ "$user_input" = "2" ]
then
    List_Dir
elif [ "$user_input" = "3" ]
then
    Show_Run
elif [ "$user_input" = "4" ]
then
    TCP_Con
elif [ "$user_input" = "5" ]
then
    Net_Int
elif [ "$user_input" = "6" ]
then
    Show_Path
elif [ "$user_input" = "7" ]
then
    Host_Name
elif [ "$user_input" = "8" ]
then
    Route_Table
elif [ "$user_input" = "9" ]
then
    Up_time
elif [ "$user_input" = "10" ]
then
    Block_Dev
elif [ "$user_input" = "11" ]
then
    Mount_Dev
else
    Main_Menu
fi

function Check_Disk
clear

freedisk=$( df -h  )

echo "$freedisk"

then
    Main_Menu

else
    CheckDisk
fi

1 个答案:

答案 0 :(得分:0)

对于菜单来说似乎有点长,我会使用类似的东西:

function f_list_nodes {
echo "some stuff"
}
function f_add_nodes {
echo "some stuff 2"
}
function f_del_nodes {
echo "some stuff 3"
}

function m_main_menu {
while [ 1 ]
do
    PS3='Choose a number: '
    select choix in "listnodes" "addnode" "delnode" "quit"
    do
        break
    done
    case $choix in
        listnodes)  f_list_nodes;;
        addnode)    f_add_node;;
        delnode)    f_del_node;;
        quit)       exit ;;
        *)      echo "nope" ;;
    esac
done
}

m_main_menu