2

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?

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

2 Comments

Omfg I feel dumb now. Thank you for pointing that out. I was trying to get that list after every command from the input was completed.. and that certainly solved it.
anyway, you case for this
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.