Skip to main content
edited body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

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. This means it'sIt 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 certainsure we have a real name for them yet. The spacecode between the end of the case statement and the enddone of the select loop will be used for code that is runruns when the user picks an invalid choice has been made from the menu (you could also do this with a *) case label). Note too that we may

It would be better served by usingto use more descriptive variable names, like name and family, and that 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 just list the strings directly).

In the code below, I've also made the code terminate with exit if the user chooses Quit (break just exits the select loop, and we use it when the user has successfully picked a name) and all. 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"; 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"

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. This means it's 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 certain we have a real name for them yet. The space between the end of the case statement and the end of the select loop will be used for code that is run when an invalid choice has been made from the menu (you could also do this with a *) case label). Note too that we may be better served by using more descriptive variable names, like name and family, and that 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 just list the strings directly).

In the code below, I've also made the code terminate with exit if the user chooses Quit (break just exits the select loop and we use it when the user has successfully picked a name) and 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"

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"
edited body
Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k
#!/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 Listlist: '
options=("Smith" "Brown" "Miller" "Manual Input"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 "Whats"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.

#!/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 -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 -p "Whats your family: " family
            ;;
        5)
            echo 'Bye!' >&2
            exit
            ;;
    esac

    echo 'Try again!' >&2

done

printf 'Welcome, %s %s!\n' "$name" "$family"
#!/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.

Source Link
Kusalananda
  • 355.8k
  • 42
  • 735
  • 1.1k

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. This means it's 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 certain we have a real name for them yet. The space between the end of the case statement and the end of the select loop will be used for code that is run when an invalid choice has been made from the menu (you could also do this with a *) case label). Note too that we may be better served by using more descriptive variable names, like name and family, and that 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 just list the strings directly).

In the code below, I've also made the code terminate with exit if the user chooses Quit (break just exits the select loop and we use it when the user has successfully picked a name) and 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 -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 -p "Whats your family: " family
            ;;
        5)
            echo 'Bye!' >&2
            exit
            ;;
    esac

    echo 'Try again!' >&2

done

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