1

I have a simple script which i want to list some pre-filled choices or for the user to enter a number to correlate with a build

This is what I have currently.

read -p "Please select from the list below or enter the build number you would like to download " build
    case {build} in
        Latest)
            build=lastSuccessful
            break;;
        *)
            break;;
    esac

The problem is that is doesn't provide a list for the user to choose.

Ideally it would look something like this

Build Choices 1) Latest 3) Successful 5) pick a build number (1-10000) 2) Stable 4) etc. Please select from the list above or enter the build number you would like to download: _

Or would it be better to do this in 2 case statements? Asking if they want to choose a particular build or enter their own.

UPDATE: After some thinking I ended up simplifying it to a yes no statement

while true;do
    read -p "Would you like to download the latest successful build? (yes/no) " yn
    case $yn in
        [Yy]*)
            echo
            build=lastSuccessfulBuild
            break;;
        [Nn]*)
            echo
            read -p "Enter the build number to download: " build
            break;;
        *) echo 'I am not going to tell you again';;
        [Qq]*) exit 0;;
    esac
done
krodami
  • 35
  • 3
  • 7
  • Print the list before the `read` statement, and then say `list above`. – Barmar Feb 09 '15 at 22:35
  • 1
    what part of `select area in area1 area2 area3` in the answer you accepted don't you understand ( http://stackoverflow.com/questions/28377618/how-do-i-create-a-shell-script-with-multiple-choice-menu-variables) . – shellter Feb 09 '15 at 22:52

2 Answers2

4

A select statement might be what you want:

select name in Latest Successful  Stable "Pick a Build Number" ;
do
  case "$name" in
        Latest)
            build=lastSuccessful
            break
          ;;
        Successful)
            build=lastSuccessful
            break
          ;;
        Stable)
            build=lastSuccessful
            break
            ;;
        Pick*)
            read -p "Enter a number from 1 to 10000: " number
            break
            ;;
  esac
done

This generates the menu:

1) Latest
2) Successful
3) Stable
4) Pick a Build Number
#?

The user can enter 1, 2, or 3 and the variable name is set to the corresponding string.

John1024
  • 109,961
  • 14
  • 137
  • 171
  • But this will limit the options the user can chose wont it? I want them to be able to select a number as well and that number can range from 1-10000 – krodami Feb 10 '15 at 03:01
  • @krodami OK. I added the option of entering a number. – John1024 Feb 10 '15 at 03:08
0

You may like dialog as well.

Chceck this out:

dialog --no-cancel --menu "Please select from the list below or enter the build number you would like to download " 11 60 12 1 Latest 2 Successful 3 Stable
pawel7318
  • 3,383
  • 2
  • 28
  • 44