Question

My script

#!/bin/bash
while :
do
    echo "[*] 1 - Create a gnome-terminal"
    echo "[*] 2 - Display Ifconfig"
    echo -n "Pick an Option > "
    read Input

    if [ "$Input" = "1" ]; then
        gnome-terminal
        sleep 3
    echo "[*] 1 - Create a gnome-terminal"
    echo "[*] 2 - Display Ifconfig"
    echo -n "Pick an Option > "
    read Input

    fi

    if [ "$Input" = "2" ]; then
        clear
        ifconfig
    echo "[*] 1 - Create a gnome-terminal"
    echo "[*] 2 - Display Ifconfig"
    echo -n "Pick an Option > "
    read Input

    fi

done

Doesn't reuse the Input variable, say if you want to run the ifconfig option multiple times in a row. It rechecks the first variable string to see if it matches "1", and when it doesn't it echoes back the list of options. Any ideas on how to make any input variable accessible regardless of when it is used?

Was it helpful?

Solution 2

Use select instead of rolling your own menu:

choices=(
    "Create a gnome-terminal" 
    "Display Ifconfig"
)
PS3="Pick an Option > "
select action in "${choices[@]}"; do 
    case $action in 
        "${choices[0]}") gnome-terminal; sleep 3;; 
        "${choices[1]}") clear; ifconfig;; 
        *) echo invalid selection ;; 
    esac
done

select gives you an infinite loop by default. If you want to break out of it, put a break statement into the case branch.

OTHER TIPS

The problem is that you're writing out the menu twice on each iteration. Stop doing that:

#!/bin/bash
while :
do
    echo "[*] 1 - Create a gnome-terminal"
    echo "[*] 2 - Display Ifconfig"
    echo -n "Pick an Option > "
    read Input

    if [ "$Input" = "1" ]; then
        gnome-terminal
        sleep 3
    fi

    if [ "$Input" = "2" ]; then
        clear
        ifconfig
    fi
done
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top