LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash srcipt select help? (http://www.linuxquestions.org/questions/programming-9/bash-srcipt-select-help-4175446452/)

dancer_69 01-20-2013 06:12 AM

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): "
        select i in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
do       
        case "$i" in
                1) echo ................

The problem is that when I run the screen all selection numbers printed:
Code:

Please enter your choice (1-20):
1) 1        3) 3        5) 5        7) 7        9) 9        11) 11        13) 13        15) 15        17) 17        19) 19
2) 2        4) 4        6) 6        8) 8        10) 10        12) 12        14) 14        16) 16        18) 18        20) 20
#?

but I want to appear on screen only the first and last line, like this:
Code:

Please enter your choice (1-20):
#?

So, is there any way to hide the numbers array?

bijo505 01-20-2013 06:45 AM

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
clear;
PS3="Enter a number between 1 and 4 : "
echo "$PS3"
echo "For exiting from loop type 500 and press Enter!!\n"
read x
case $x in
        1) echo "The entered number is $x" ;;
        2) echo "The entered number is $x" ;;
        3) echo "The entered number is $x" ;;
        4) echo "The entered number is $x" ;;
        500) echo "it is the time to say Good Bye!!"; exit ;;
        *) echo "The entered number is \"$x\" and it is out of range\n";. $0 ;;
esac


dancer_69 01-20-2013 06:56 AM

This works perfectly!
Thanks a lot.

dancer_69 01-20-2013 11:57 AM

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
clear;
method()
{
PS3="Enter a number between 1 and 4 : "
echo "$PS3"
echo "For exiting from loop type 500 and press Enter!!\n"
read x
case $x in
        1) echo "The entered number is $x" ;;
        2) echo "The entered number is $x" ;;
        3) echo "The entered number is $x" ;;
        4) echo "The entered number is $x" ;;
        500) echo "it is the time to say Good Bye!!"; exit ;;
        *) echo "The entered number is \"$x\" and it is out of range\n";. $0 ;;
esac
}
while :
do
    method
done


David the H. 01-20-2013 04:27 PM

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}

dancer_69 01-20-2013 04:41 PM

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 11:03 AM.