LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 01-20-2013, 07:12 AM   #1
dancer_69
LQ Newbie
 
Registered: May 2011
Posts: 6

Rep: Reputation: Disabled
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?
 
Old 01-20-2013, 07:45 AM   #2
bijo505
Member
 
Registered: Nov 2012
Location: Bangalore
Distribution: Fedora & Ubuntu
Posts: 77

Rep: Reputation: 18
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
 
Old 01-20-2013, 07:56 AM   #3
dancer_69
LQ Newbie
 
Registered: May 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
This works perfectly!
Thanks a lot.
 
Old 01-20-2013, 12:57 PM   #4
dancer_69
LQ Newbie
 
Registered: May 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
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

Last edited by dancer_69; 01-20-2013 at 01:16 PM.
 
Old 01-20-2013, 05:27 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950Reputation: 1950
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}

Last edited by David the H.; 01-20-2013 at 05:29 PM. Reason: bad tag, bad!
 
2 members found this post helpful.
Old 01-20-2013, 05:41 PM   #6
dancer_69
LQ Newbie
 
Registered: May 2011
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.

Last edited by dancer_69; 01-20-2013 at 05:42 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with bash select and case neocontrol Programming 1 01-03-2013 10:42 PM
[SOLVED] bash: format select options dokkalf Programming 7 06-06-2011 09:45 PM
bash: if block inside select' agrestic Linux - Software 1 01-14-2009 05:01 PM
Bash select statement and spaces meshcurrent Linux - General 2 04-09-2003 10:35 AM
bash: can't get out of SELECT adam_boz Programming 2 09-17-2002 12:53 AM


All times are GMT -5. The time now is 07:26 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration