UID0
UID0

Reputation: 5

Bash - create menu, read variables from keyboard

I'm trying to build a menu in Bash:

echo Option1 echo Option2 echo Option3 echo Option4 ... Option3 = ls -l /home/test/|wc -l

Now, if the users selects option 3 for instance, I would like bash to call a Linux command specific to Option 3.

I've been able to read what users enters from the keyboard, however I'm not able to link the Option 3 for instance with the number 3 that user enters on the keyboard:

$ Which option would you like: 3 You have selected Option3. Would you like to continue?Y

You have 20 files in the home directory.

Something like that.

Upvotes: 1

Views: 2257

Answers (2)

paxdiablo
paxdiablo

Reputation: 881653

Assuming you're using read to get the user input, it shows up (by default) in the REPLY variable, which you can then test with something like:

read
if [[ "${REPLY}" = "3" ]] ; then
    echo You entered 3
fi

If you want your input in a different variable, you can just supply that with the commend:

read userinput
if [[ "${userinput}" = "3" ]] ; then
: : (and so on)

If there are lots of possibilities, you're probably better off using case instead of a lot of if/else lines, an example which is shown below:

case "${userinput}" in
    0)
        echo You entered zero.
        ;;
    [1-9]*)
        echo You entered something starting with one through nine.
        ;;
    *)
        echo What?
        ;;
esac

Upvotes: 2

hackerb9
hackerb9

Reputation: 1932

select

It sounds like what you want is bash's select command which shows you a numbered list of items and then gets a response.

Example

select name in "option 1" "option 2" "option 3"
do
    echo "You picked $name"
    break
done
./select.sh 
1) option 1
2) option 2
3) option 3
#? 3
You picked option 3

Here is what the Bash manpage has to say about select:

select name [ in word ] ; do list ; done

The list of words following in is expanded, generating a list of items. The set of expanded words is printed on the standard error, each preceded by a number. If the in word is omitted, the positional parameters are printed. The PS3 prompt is then displayed and a line read from the standard input. If the line consists of a number corresponding to one of the displayed words, then the value of name is set to that word. If the line is empty, the words and prompt are displayed again. If EOF is read, the command completes. Any other value read causes name to be set to null. The line read is saved in the variable REPLY. The list is executed after each selection until a break command is executed. The exit status of select is the exit status of the last command executed in list, or zero if no commands were executed.

A better example of select

#!/bin/bash

options+=("butter" "scotch" "tape" "monkey" "nut")
options+=("some atrociously long option")

select name in "${options[@]}"
do
    if [[ "$name" ]]; then
        echo "You selected $name"
    else
        echo "You typed in: $REPLY"
        name=$REPLY
    fi

    case "$name" in
        butter) echo "I say butterscotch"
                ;;
        scotch) echo "I say scotchtape"
                ;;
        tape) echo "I say tapemonkey"
                ;;
        monkey) echo "I say monkeynut"
                ;;
        nut) echo "I say nutbutter"
                ;;
        "some atrocious"*)
            echo "You win!"
            break
            ;;
        *)
            echo "I'm stumped. Goodbye."
            break
            ;;
    esac        
done
$ ./select.sh 
1) butter                        4) monkey
2) scotch                        5) nut
3) tape                          6) some atrociously long option
#? 3

You selected tape
I say tapemonkey
#? 4

You selected monkey
I say monkeynut
#? nut

You typed in: nut
I say nutbutter
#? butternut

You typed in: butternut
I'm stumped. Goodbye.
$ 

Upvotes: 0

Related Questions