Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[BASH] Stuck on a list.

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
ReShout
Posts: 23
Joined: 2022-01-15 11:50
Location: Germany
Has thanked: 1 time

[BASH] Stuck on a list.

#1 Post 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!!

User avatar
jby
Posts: 13
Joined: 2022-01-18 13:39
Contact:

Re: [BASH] Stuck on a list.

#2 Post by jby »

If the example is verbatim then there's a syntax error on line 16 where a " is missing before the 4...

ReShout
Posts: 23
Joined: 2022-01-15 11:50
Location: Germany
Has thanked: 1 time

Re: [BASH] Stuck on a list.

#3 Post 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.

User avatar
jby
Posts: 13
Joined: 2022-01-18 13:39
Contact:

Re: [BASH] Stuck on a list.

#4 Post 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
Last edited by jby on 2022-01-18 21:27, edited 1 time in total.

Post Reply