I'm creating a bash script and I want to create a m e n u. I am a beginner and still a noob but appreciate all your help! When I run the code it displays the menu but the problem is when the user enters a choice it keeps re-printing the menu to ask again and again no matter what choice was selected.

You can enable tracing in a shell script: replace the first line of the script by #!/bin/bash -x. You can enable and disable tracing by the command set -x and set +x, too. Information to the user should be written to standard error stream (stderr). This can be done by redirecting the echo output, e.g. echo "Main Menu:" >&2. Now it is possible to pipe the data output of your script to another command. If you really need a menu in a shell script than you can use the shell command select.
–
miracle173Mar 24 '14 at 6:18

1

Please don't change the meaning of your question like that. If you have a new issue, post a new question instead.
–
terdonMar 26 '14 at 15:23

3 Answers
3

The first step whenever you are debugging something is to have your script print the various variables it is using. For example, here, if you add an echo $choicebefore the case statement, you will see that it will print 0 no matter what value you give it.

This is because you're using the -i option to declare which does (from help declare):

-i to make NAMEs have the `integer' attribute

So, you are declaring that variable to be an integer and then are passing it letters (strings) and since bash is expecting an integer, it converts it to a 0.

The next issue is that you are running a while loop that will only exit when $choice is 5. Even if the rest of your syntax were OK, that would only happen for choice e.

Here is a working example of your script. I have removed the declare which is unnecessary.

It's all done by this one-line command, and because 9menu displays the menu graphically, there is no need to add an "invalid answer" error condition, because the menu entries are selected from the Main Menu window with a mouse click, and there are only five possible entries in the menu that can be selected by clicking them with the mouse.

I suggest that you make the following improvement to your original menu idea. Instead of displaying "You entered e" when you click Exit, why not use the exit menu item to close the Main Menu window and exit from 9menu? To do it that way use the following command:

maybe this is be a useful comment but it does not explain why the script does not work.
–
miracle173Mar 24 '14 at 6:23

@miracle173 Both of the scripts work. The first script is written to the specifications stated in the question. The second script has an improvement over the original design stated in the question, which gives it more user-friendly exit condition. You claim that the script does not work. Did you first run sudo apt-get install 9menu before running the script? Remember, this is a shell script, so the terminal program that is required must first be installed.
–
karelMar 24 '14 at 6:37

3

your answer does not explain why the script of the OP does not work
–
miracle173Mar 24 '14 at 7:19

@miracle173 You're a mathematician with rep as one, so I'm trying to give this to you completely logically without appearing to be condescending in any way. The bash code in the question is 37 lines of poorly constructed code. The bash code in my answer is 1 line of correctly constructed bash code. My explanation to a mathematician is that my code is designed to be concise, to be accurate and to formulate the assignment correctly without any need for any additional proofs by counterexample. I'm asking you to be charitable and consider it from my point of view.
–
karelMar 24 '14 at 7:37

2

I have to say I agree with @miracle173. Your code just introduces a needless dependency for what is an extremely simple task and you assume that GUI is better while you have no knowledge of what the script will be for. This was a bash question and you bring in an X program which immediately makes it harder to install, less portable and heavier. It also does not answer the actual question which was "what's wrong with my script"? While this might be a useful tool, you don't explain how this can be integrated into the script, how will the OP capture the user's choice and process it in her script?
–
terdonMar 26 '14 at 15:21