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

3
  • 1
    Can you share with us what have you tried? Commented Jan 16, 2021 at 1:11
  • Did you use the select builtin to create your menu? Commented Jan 16, 2021 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. Commented Jan 16, 2021 at 10:20

1 Answer 1

0

You could do something like

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

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.