1

I'm learning Bash and I am trying to solve an exercise.

The exercise consists of selecting the first and last names, from a menu list. However, if the name is not in the list then set manual input.

But I'm a little bit confused because I don't when an option is select how to go to the second menu, without select option Quit (I try add Break in any option but doesn't work).

For the manual input y try read the input from the user but doesn't see working any suggestion?

How I can terminate the loop when I get the name and last name? Because when I get both, I get to the same loop.

echo -e "Choose your name Exercise #2 \n"
PS3='Please enter your name from the List: '
options=("Elvis" "John" "Mark" "Manual Input" "Quit")
select opt in "${options[@]}"
do
    case $opt in
        "Option 1")
            ;;
        "Option 2")
            ;;
        "Option 3")
            ;;
        "Option 4")
            echo "Sorry your Name is not in the List Give your name"
            read -e -p "Whats your name: " MANUALINPUT
            ;;
        "Quit")
            break
            ;;
   esac

echo "Hello $opt Next Step is Select your Lastname "
done

PS4='Please enter your last name from the List: '
options2=("Smith" "Brown" "Miller" "Manual Input" "Quit")
select opt2 in "${options2[@]}"
do
    case $opt2 in
        "Option 1")
            ;;
        "Option 2")
            ;;
        "Option 3")
            ;;
        "Option 4")
            echo "Sorry your Name is not in the List Give your name"
            read -e -p "Whats your name: " MANUALINPUT
            ;;
        "Quit")
            break
            ;;
   esac

clear
echo "Welcome $opt $opt2"
done
0

1 Answer 1

1

The select loop would set opt to the option that the user picked, and it would set $REPLY to what the user typed. So if the user typed 1 as a response to your first question, $REPLY would be 1 and $opt would be Elvis. At no point would $opt be Option 1. It makes it easier to write your case statements for testing $REPLY rather than $opt.

Also, it's too early to greet the person by name inside the select loop, as we may still not be sure we have a real name for them yet. The code between the end of the case statement and the done of the select loop runs when the user picks an invalid choice from the menu (you could also do this with a *) case label).

It would be better to use more descriptive variable names, like name and family, and we don't need two separate arrays to store the options (in fact, it's questionable if you need arrays at all in this case as we could list the strings directly).

In the code below, I've also made the code terminate with exit if the user chooses Quit (break exits the select loop, and we use it when the user has successfully picked a name). All interactive dialogue happens on the standard error stream, as is common.

#!/bin/bash

PS3='Please enter your name from the list: '
options=("Elvis" "John" "Mark" "Manual input" "Quit")
select name in "${options[@]}"; do
    case $REPLY in
        1|2|3)
            break       # user picked name from list
            ;;
        4)
            echo 'Sorry your name is not in the list' >&2
            read -e -r -p "What's your name: " name
            break
            ;;
        5)
            echo 'Bye!' >&2
            exit
            ;;
   esac

   echo 'Try again!' >&2

done
printf 'Hello %s, now select your family name\n' "$name" >&2

PS3='Please enter your family name from the list: '
options=("Smith" "Brown" "Miller" "Manual input" "Quit")
select family in "${options[@]}"; do
    case $REPLY in
        1|2|3)
            break       # user picked name from list
            ;;
        4)
            echo 'Sorry your family is not in the list' >&2
            read -e -r -p "What's your family: " family
            ;;
        5)
            echo 'Bye!' >&2
            exit
            ;;
    esac

    echo 'Try again!' >&2

done

printf 'Welcome, %s %s!\n' "$name" "$family"

Other small things fixed: Use -r with read to be able to read backslashes properly, and you used PS4 instead of PS3 for setting the selection prompt in the second loop.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .