LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 04-27-2005, 08:35 AM   #1
zidane_tribal
Member
 
Registered: Apr 2005
Location: chained to my console.
Distribution: LFS 6.1
Posts: 143

Rep: Reputation: 18
Question bash script: using "select" to show multi-word options? (like "option 1"/"o


heya

hit a bit of a problem. im currently attempting to learn bash scripting, with the aim of making a little menu script for a freind. i want to use "select" within my script, but i cant seem to figure out how to get it to list many-word options.

an example might be in order.

if i use this script:

Code:
#!/bin/bash
options1="option1 option2 quit"

echo options1 $options1

select opt in $options1 ; do
echo $opt
if [ "$opt"="quit" ]; then
        exit
fi
done
i get this output:

Code:
1) option1
2) option2
3) quit
#? 3
which is nice.... but, what i really want, is to get something like this output (i just typed this out, i cant make select output this)

Code:
1) option 1
2) option 2
3) quit
#? 3
(note the space between "option" and "number")
when i tried to make something like that, using this script:

Code:
#!/bin/bash
options1="\"option 1\" \"option 2\" \"quit\""

echo options1 $options1

select opt in $options1 ; do
echo $opt
if [ "$opt"="quit" ]; then
        exit
fi
done
i get output like this:

Code:
1) "option
2) 1"
3) "option
4) 2"
5) "quit"
#? 5
now, i know that i can have multiword options if i specify them in the select line (like: select opt in "option 1" "option 2" "quit"), but i would like to specify them as a variable. it just seems neater that way.

so, the question becomes, how do i make select see the multiple word options held in my variable?

many thanks in advance to any guidance you can give.
 
Old 04-27-2005, 09:18 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,523

Rep: Reputation: 365Reputation: 365Reputation: 365Reputation: 365
Try that one:

Code:
#!/bin/bash
options1="\"option 1\" \"option 2\" \"quit\""

echo options1 $options1

eval set $options1
select opt in "$@"
do
echo $opt
if [ "$opt"="quit" ]; then
        exit
fi
done
 
Old 04-27-2005, 09:27 AM   #3
zidane_tribal
Member
 
Registered: Apr 2005
Location: chained to my console.
Distribution: LFS 6.1
Posts: 143

Original Poster
Rep: Reputation: 18
w00t

works a treat

thanks jlliagre, much appreciated
 
Old 07-24-2007, 12:03 PM   #4
gloriant
Member
 
Registered: Sep 2004
Distribution: Debian{Woody,Sarge,Etch}, UbuntuLTS6.06, SuSE{6.2,8.0}
Posts: 42

Rep: Reputation: 15
you could also use the bash-capability for arrays:
Code:
#!/bin/bash

#define options as array
declare -a options

#set first empty position with new value
options[${#options[*]}]="option 1";
options[${#options[*]}]="option 2";
options[${#options[*]}]="quit";

#expand to quoted elements:
select opt in "${options[@]}"; do
  case ${opt} in
  ${options[0]}) echo "Polly wants a cookie"; ;;
  (quit) break; ;;
  (*) echo "${opt}"; ;;
  esac;
done
 
Old 10-25-2011, 03:20 PM   #5
8#4247
LQ Newbie
 
Registered: Oct 2011
Posts: 1

Rep: Reputation: Disabled
Smile The following would also do...... :)

Code:
#!/bin/bash
options="option 1,option 2,quit"
OIFS=$IFS  # Save the current IFS (Internal Field Separator)
IFS=','    # New IFS
select opt in $options
do
echo $opt
if [[ "$opt" == "quit" ]]
then
IFS=$OIFS  # Restore the IFS
exit
fi
done

Last edited by 8#4247; 10-25-2011 at 03:21 PM.
 
Old 10-26-2011, 02:52 PM   #6
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
You should take care when resurrecting old threads. It should generally only be done when you have something significant to add to them. And make sure you clearly point out that you are doing so, otherwise people tend to overlook the dates and start replying to a long-dead conversation.

That said, you did post something significant. But I'd still say arrays are the way to go. Resetting the IFS works, but to my mind is best used to convert strings into arrays for further use.

(Arrays are painfully underutilized in bash scripting, IMO. Any time you have a list of strings to work on, filenames, options, etc., put it into an array first.)

And now to add my own observation...this part of gloriant's post above can be made much cleaner:
Code:
#set first empty position with new value
options[${#options[*]}]="option 1";
options[${#options[*]}]="option 2";
options[${#options[*]}]="quit";
This is an awkward way to add elements to an array. You can use this instead:

Code:
options+=( "option 1" "option 2" "quit" )
array+=() tells it to append the elements to an existing array, while array=() will reset it with the new values. Either one could be used here, since you're starting with an empty array anyway.

Combine this with IFS to split a string stored in a variable:
Code:
optionstring="option 1,option 2,quit"

IFS=","
options=( $optionstring )
IFS=$' \t\n'
Notice that this is one of the rare instances where you do not want to quote the variable. The last line also demonstrates how you can manually reset IFS to the default value ($'' is ansi-c style quoting. See the QUOTING section of the bash man page).

It's also possible to simply unset IFS, and the shell will behave as if it were set to the default.
 
Old 03-21-2013, 11:35 AM   #7
maine_guy
LQ Newbie
 
Registered: Nov 2010
Posts: 6

Rep: Reputation: 0
Replying to ancient threads

I see nothing wrong with replying to ancient threads. Perhaps the OP is no longer around but people searching for answers will certainly find these old relics and a new reply could help them.
 
  


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
Shell Script: Find "Word" Run "Command" granatica Linux - Software 5 07-25-2007 08:42 AM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 10:00 AM
Can't install "glibmm" library. "configure" script can't find "sigc++-2.0&q kornerr Linux - General 4 05-10-2005 03:32 PM
bash-2.05b# Xlib: extension "XFree86-DRI" missing on display ":0.0". citrus Linux - General 8 02-22-2004 11:43 AM
anging "Protocol" option to "IMPS/2" in XF86Config-4 causes problems zstingx Linux - General 2 10-27-2003 10:47 AM


All times are GMT -5. The time now is 03:23 AM.

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