Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

OS - Linux 64-bit

I am trying to implement a simple menu using select command. The script (for testing purposes) is as follows

#!/bin/bash
echo "*******************"
PS3='Select an option and press Enter: '
options=("apache" "named" "sendmail")
select opt in "${options[@]}"
do
  case $opt in
        "apache")
          date
          ;;
        "named")
          echo "test"
          ;;
        "sendmail")
          echo "test 2"
          ;;
        *) echo "invalid option";;
  esac
done
echo "*********************"

The script is not recognizing any valid inputs I give and always prints the "invalid option" on screen. What am doing wrong in this script.

share|improve this question
3  
Your input is 1, 2 or 3, right? –  enzotib Sep 14 '11 at 12:46

2 Answers 2

up vote 3 down vote accepted

Your menu shown by select will look like:

1) apache
2) named
3) sendmail
Select an option and press Enter: 

At this time, you enter "1" or "2" or "3": you don't type the word.

Also, select will look until it sees a break command, so you want this:

  case $opt in
        "apache")
          date
          break
          ;;
        "named")
          echo "test"
          break
          ;;
        "sendmail")
          echo "test 2"
          break
          ;;
        *) echo "invalid option";;
  esac

If you wanted to allow the user to enter either the number or the word, you could write this:

select opt in "${options[@]}"; do
  case "$opt,$REPLY" in
    apache,*|*,apache)     do something; break ;;
    named,*|*,named)       do something; break ;;
    sendmail,*|*,sendmail) do something; break ;;
  esac
done

The comma has no syntactical significance, it's just there to be able to pattern match on either the $REPLY variable (which is what the user actually typed) or the $opt variable

share|improve this answer
    
The select command needs a break, not case. –  glenn jackman Sep 14 '11 at 18:09
    
Thanks! I did not realize that it was numerical options. Duhh! –  Sumod Sep 15 '11 at 10:45

Your $opt variable will be set to the option word corresponding to the number that the user inputs. If you want to look at what the user actually typed, look at $REPLY.

share|improve this answer
1  
Given that the case compares $opt against "apache", "named" and "sendmail", it is correct and nothing has to be changed. –  manatwork Sep 14 '11 at 14:13
    
@manatwork, unless the OP wants the user to type in the words "apache", "named" or "sendmail" rather than pressing 1, 2 or 3. –  Kusalananda Sep 14 '11 at 14:38
    
Interesting. $REPLY holds 1/2/3, and $opt holds apache/named/sendmail –  Peter.O Sep 15 '11 at 6:50
    
@fred, yes, $REPLY is whatever the user typed, and if it was a valid number then $opt will be the word corresponding to that number. –  Kusalananda Sep 15 '11 at 6:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.