Page 1 of 1

[BASH] Stuck on a list.

Posted: 2022-01-17 17:49
by ReShout
Hello everyone!
I am working on a mini software which will be written in BASH and I encountered a problem.
I want the program to do this:
Show a list of software (names);
Let you choose the software from the list
echo "$REPLY"
then

Code: Select all

git clone link
Here is the code:

Code: Select all

PS3="Choose a software to download."
options=("1" "2" "3" "4" "5" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "1" )
            echo "1"
            ;;
        "2")
            echo "2"
            ;;
        "3")
            echo "3"
            ;;
        "4")
            echo 4"
            ;;
        "5")
            echo "5"
            ;;
        "Quit")
            echo "Quit"
            break
            ;;
    *) echo "Invalid Option $REPLY";;
    esac
done
THE PROBLEM
I tried to add the [git clone] command using the && and || operators, the code was git clonning before the list even appeared.
How do I fix this mess of a code XD

Thanks in advance!!

Re: [BASH] Stuck on a list.

Posted: 2022-01-18 13:53
by jby
If the example is verbatim then there's a syntax error on line 16 where a " is missing before the 4...

Re: [BASH] Stuck on a list.

Posted: 2022-01-18 15:12
by ReShout
jby wrote: 2022-01-18 13:53 If the example is verbatim then there's a syntax error on line 16 where a " is missing before the 4...
Try replacing the options with something different.

Re: [BASH] Stuck on a list.

Posted: 2022-01-18 21:21
by jby
Yeah, well the "${options[@]}" part requires numbers, so if you try something else than numbers it's not going to work.
AND, it requires numbers from 1, since it's the index in the array, so if you were to use 6,7,8,9 - you'd still need to index them by 1,2,3,4...
I.e. the first option will always be number 1
Tip: Try entering "6" and see what happens...
What you could do is change the output from the different options:

Code: Select all

PS3="Choose a software to download."
options=("1" "2" "3" "4" "5" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "1" )
            echo "one"
            ;;
        "2")
            echo "two"
            ;;
        "3")
            echo "three"
            ;;
        "4")
            echo "four"
            ;;
        "5")
            echo "five"
            ;;
        "Quit")
            echo "Quit"
            break
            ;;
    *) echo "Invalid Option $REPLY";;
    esac
done