![]() |
bash srcipt select help?
Hello,
I'm new to bash scripting and I've made a script with selection menu, which work fine, but there is a thing which I want to change. I'm using select command like this: Code:
cho "Please enter your choice (1-20): "Code:
Please enter your choice (1-20): Code:
Please enter your choice (1-20): |
Hi Dancer_69,
If you want to use "select", it will print the menu, but you can avoid that by using the following. Code:
#!/bin/bash |
This works perfectly!
Thanks a lot. |
I spoke to fast.
This way the script exits after you choose any of the options. I want to loop until the quit option selected, and only then to exit. EDIT: I found how to do it!! Code:
#!/bin/bash |
select is a special, pre-defined kind of loop, designed to do one simple job; create a simple menu out of the list of items you give it. For more flexibility, the more general while true loop is needed, as you've discovered.
In both cases, you usually use the break and continue keywords to control the function of the loop. exit, OTOH, leaves the script entirely. You only need to set PS3 with select, because it uses it for its prompt. For the while loop, just echo the line directly. You can also use read's -p option. Well done using a function, by the way. Most new scripters don't get that fancy. PS: Instead of listing out a whole a bunch of numbers by hand, you can use brace expansion. Code:
select i in {1..20} |
Thanks for the tips.
About the function, I just take the idea from a complex script. I have a little experience on android programing, so when I saw the "method()" field with the { to follow, it remind me the java/android methods which have the same structure. After that was quite easy for me to use it. |
| All times are GMT -5. The time now is 04:22 PM. |