0

So I created a menu so I could control my VPN app without having to enter commands again and again.

However, I'd like to be able to select eg number 7, press enter, then enter a command manually whilst within the menu. When the command executes, I want it to return to the menu.

I have tried the read command but no luck so far.

Thanks,

Edit - repurposing this code that I sourced from DevDojo. As requested:

#!/bin/bash

##
# BASH menu script that checks:
#   - Memory usage
#   - CPU load
#   - Number of TCP connections 
#   - Kernel version
##

server_name=$(hostname)

function memory_check() {
    echo ""
    echo "Memory usage on ${server_name} is: "
    free -h
    echo ""
}

function cpu_check() {
    echo ""
    echo "CPU load on ${server_name} is: "
    echo ""
    uptime
    echo ""
}

function tcp_check() {
    echo ""
    echo "TCP connections on ${server_name}: "
    echo ""
    cat  /proc/net/tcp | wc -l
    echo ""
}

function kernel_check() {
    echo ""
    echo "Kernel version on ${server_name} is: "
    echo ""
    uname -r
    echo ""
}

function all_checks() {
    memory_check
    cpu_check
    tcp_check
    kernel_check
}

##
# Color  Variables
##
green='\e[32m'
blue='\e[34m'
clear='\e[0m'

##
# Color Functions
##

ColorGreen(){
    echo -ne $green$1$clear
}
ColorBlue(){
    echo -ne $blue$1$clear
}

menu(){
echo -ne "
My First Menu
$(ColorGreen '1)') Memory usage
$(ColorGreen '2)') CPU load
$(ColorGreen '3)') Number of TCP connections 
$(ColorGreen '4)') Kernel version
$(ColorGreen '5)') Check All
$(ColorGreen '0)') Exit
$(ColorBlue 'Choose an option:') "
        read a
        case $a in
            1) memory_check ; menu ;;
            2) cpu_check ; menu ;;
            3) tcp_check ; menu ;;
            4) kernel_check ; menu ;;
            5) all_checks ; menu ;;
        0) exit 0 ;;
        *) echo -e $red"Wrong option."$clear; WrongCommand;;
        esac
}

# Call the menu function
menu

4
  • 1
    Can you share with us what have you tried? – schrodigerscatcuriosity Jan 16 at 1:11
  • Did you use the select builtin to create your menu? – icarus Jan 16 at 10:11
  • Hi all - I have added the template that I used (changes the functions to match my needs). I will try the answer from waltinator too. – jtamm Jan 16 at 10:20
  • thanks to waltinator for the answer!! – jtamm Jan 16 at 14:14
0

You could do something like

read -p 'Cmd? ' cmd
eval $cmd
  
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.