You are not logged in.

#1 2011-04-05 16:26:19

OrionFyre
Member
Registered: 2008-03-16
Posts: 68

[SOLVED][BASH] Using a string to pass multiple arguments.

I'm scratching my brain on this little project my friend roped me into.

I have a function which generates a list of VM's and their states and stores it as a variable. this list is to be presented to the user using the libgui.sh 'ask_option' function. The problem is that the ask_option function does not interpret it correctly. And by saying ask_option isn't doing it right I mean I am expecting a certain behavior, and that expectation is flawed.

Here are the two parts in question

#assume VMLIST has a value identical to this assignment after my function call:
#I have confirmed that this is the exact value
VMLIST="\"Arch Linux Live\" \"running\" \"Windows XP\" \"power off\"" 

ask_option  0 "VMs Present" '' optional "0" "Return to the Main Menu" $VMLIST

What I actually get shows that ask_option is interpreting each discreet word as an argument. whereas I want the argument to be the text between each quote

0            Return to Main Menu
"Arch        Linux
Live"        "running"
"Windows     XP"
"power       off"

Obviously what I want is a two column output in the dialog that resembles

0                  Return to Main Menu
Arch Linux Live    running
Windows XP         power off

What should our approach be to rectify this? How can I use the variable to suppply discreet arguments to another function? or rather how can I have the value of the variable interpreted literally on the function call line?

Last edited by OrionFyre (2011-04-05 19:04:49)

Offline

#2 2011-04-05 18:00:57

pyroscope
Member
Registered: 2011-03-11
Posts: 33
Website

Re: [SOLVED][BASH] Using a string to pass multiple arguments.

$ dumpargs() { for i in "$@" ; do echo $i ; done ; }
$ dumpargs $VMLIST
"Arch Linux Live" "running" "Windows XP" "power off"
$ eval dumpargs $VMLIST
Arch Linux Live
running
Windows XP
power off

Offline

#3 2011-04-05 18:14:13

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: [SOLVED][BASH] Using a string to pass multiple arguments.

Use an array.

options=("Arch Linux Live" "running" "Windows XP" "power off")
ask_option ... "${options[@]}"

Offline

#4 2011-04-05 18:27:10

OrionFyre
Member
Registered: 2008-03-16
Posts: 68

Re: [SOLVED][BASH] Using a string to pass multiple arguments.

ok... I get the logic.

But it's still behaving exactly the same...

ask_option 0 "VMs Present" '' required "0" "Return to Main Menu" `dumpargs $VMLIST`

Am I doing something completely wrong?

Offline

#5 2011-04-05 18:59:46

OrionFyre
Member
Registered: 2008-03-16
Posts: 68

Re: [SOLVED][BASH] Using a string to pass multiple arguments.

ARRAYS! I knew that sad

Thanks scary face kitteh! wink

I should have remembered arrays. alas. that's what happens when you don't use it for a while.

Offline

Board footer

Powered by FluxBB