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 Code:
1) option1 Code:
1) option 1 when i tried to make something like that, using this script: Code:
#!/bin/bash Code:
1) "option 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. |
Try that one:
Code:
#!/bin/bash |
w00t
works a treat :) thanks jlliagre, much appreciated :) |
you could also use the bash-capability for arrays:
Code:
#!/bin/bash |
The following would also do...... :)
Code:
#!/bin/bash |
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 Code:
options+=( "option 1" "option 2" "quit" ) Combine this with IFS to split a string stored in a variable: Code:
optionstring="option 1,option 2,quit" It's also possible to simply unset IFS, and the shell will behave as if it were set to the default. |
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.
|
use of variable PS3 (answering old thread)
The output
#? will be suppressed if you change the settings for your PS3. At shell, enter: set | grep PS This will output the settings for your shell prompt and 3 other variables (one of which -"PS4" - is to hold your setting preferences for debugging) and PS3, which is the one you need to set for your select script. In your script, add the line: PS3='<message to prompt user input/ selection>' **First time EVER that I have found a forum question to which I could contribute something AND be reasonably sure of my facts, as I have just come from learning the basic usage of "select" ****smiles broadly******* |
All times are GMT -5. The time now is 10:34 PM. |