简体   繁体   中英

Bash Script Menu Loop

I have created a menu function and have been able to take input and call the function when it is input. I am just wondering how I should make it call the menu again after I finish a function.

I have an example here of what I am looking for. I have a menu and the functions from it. I would just like to be able to loop back to the menu after using one of the functions. It may be a simple fix, but I have looked many places and cannot find what I am looking for.

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

Seems a bit long for a menu, I'd use something like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM