2

I am new to bash and I am trying to learn it from online tutors. When I tried the below code on my computer(windows) with an xserver I am getting

1) Hello
2) Quit
#?
bad option
#?

and this continue infinitely, which I am not supposed to get. I used ./programname.txt to run the file and my code is

#!/bin/bash
OPTIONS="Hello Quit"
select opt in $OPTIONS; do
   if [ "$opt" = "Quit" ]; then
       echo done
       exit
   elif [ "$opt" = "Hello" ]; then
       echo Hello World
   else
       clear
       echo bad option
   fi
done    

someone please help.

Thanks in advance

5
  • You should (un)format your question Commented Oct 6, 2014 at 8:14
  • What do you type exactly when you get this bad option? Commented Oct 6, 2014 at 8:25
  • @RaFD I didnt type anything since its by an echo result Commented Oct 6, 2014 at 8:33
  • What are you entering at the select prompt? Hello/Quit or 1/2? It's a common mistake to enter the text in options, namely Hello or Quit in your example. You should enter 1 or 2. Commented Oct 6, 2014 at 8:40
  • 1
    @RaFD thanks a lot, I understood it only now Commented Oct 6, 2014 at 11:27

1 Answer 1

2

The man page of select command clearly states where you when wrong

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 (see PARAMETERS below). The PS3 prompt is then displayed and a line read from the stan- dard 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 dis- played again. If EOF is read, the command completes. Any other value read causes name to be set to null.

select opt in $OPTIONS; do

Here OPTIONS represent the word in the syntax. Now When the OPTIONS is expanded, the list {Hello, Quit} is obatained which is printed as output, presceded by a number, as in the earlier part of the output.

1) Hello
2) Quit

Then the PS3 variable , in your system it is #? is displayed and waits for a user input. The select expects an input 1 or 2 when you press 1 the name(in syntax) ,ie in your program opt will take the value Hello. It matches the first if which prints the output as Hello world This continues until an EOF is encountered.

In the program, the exit in Quit causes an exit when 2 is pressed.

Expected output:

1) Hello
2) Quit
#? 1   <= my input
Hello World
#? 2   <= my input
done
Sign up to request clarification or add additional context in comments.

4 Comments

This doesn't explain what's actually wrong and is very hard to follow
@nu11pO1n73R thanks a lot I understood the program. Thanks for the help
@PeterR There is nothing wrong in the program. The problem was with the input provided, when the program was rum
Wait... They just pressed enter... Without entering anything?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.