You are not logged in.
Hi all!
I have two arrays like this:
SERVERS=('192.168.1.2' '192.168.1.3' '192.168.1.4' '192.168.1.n')
DESCRIPTION=('Home' 'Office' 'Other' 'Description_n')
I'd like to show an Xdialog menu (using --menubox switch) like this:
---------------------------------------------
192.168.1.2 - Home
192.168.1.3 - Office
192.168.1.4 - Other
---------------------------------------------
[ OK ] [Cancel]
I know how to create menu but I just dont know how to pass array elements to Xdialog. Is it possible?
Xdialog --ok-label 'OK' --cancel-label 'Cancel' --menubox 'Server Selection' 300x160 '${#SERVERS[@]}' ................
Offline
I don't use arrays much, but this works
SERVERS=('192.168.1.2' '192.168.1.3' '192.168.1.4' '192.168.1.n')
DESCRIPTION=('Home' 'Office' 'Other' 'Description_n')
MYARRAY=()
for ((i=0; i < ${#SERVERS[@]} ; i++)); do
MYARRAY=( ${MYARRAY[@]} ${SERVERS[$i]} ${DESCRIPTION[$i]} )
done
Xdialog --menubox Choose 20 100 1 ${MYARRAY[@]}
Xdialog seems to take parameters in a very different format from what your post has. First, the dimensions should be separate parameters, not "300x160". Also, if you enclose a variable reference in single quotes as you have done, bash should not expand it.
NOTE: I would be surprised if there wasn't some simpler/handier way to 'interleave' arrays in bash that could be used instead of this for loop. But this should get the job done.
Last edited by Trilby (2012-03-23 00:27:27)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
I don't use arrays much, but this works
SERVERS=('192.168.1.2' '192.168.1.3' '192.168.1.4' '192.168.1.n') DESCRIPTION=('Home' 'Office' 'Other' 'Description_n') MYARRAY=() for ((i=0; i < ${#SERVERS[@]} ; i++)); do MYARRAY=( ${MYARRAY[@]} ${SERVERS[$i]} ${DESCRIPTION[$i]} ) done Xdialog --menubox Choose 20 100 1 ${MYARRAY[@]}
[snip]
NOTE: I would be surprised if there wasn't some simpler/handier way to 'interleave' arrays in bash that could be used instead of this for loop. But this should get the job done.
Here is another way to interleave but not in bash:
Xdialog --menubox Choose 20 100 1 $(paste -d ' ' <(tr ' ' '\n' <<< "${SERVERS[@]}") <(tr ' ' '\n' <<< "${DESCRIPTION[@]}"))
But it's not always safe with some characters, such as spaces. That will require more work. In the sample data, this will work just fine.
Personally, I will prefer your loop version, it's more clean about what it is doing.
Offline
I don't use arrays much, but this works
SERVERS=('192.168.1.2' '192.168.1.3' '192.168.1.4' '192.168.1.n') DESCRIPTION=('Home' 'Office' 'Other' 'Description_n') MYARRAY=() for ((i=0; i < ${#SERVERS[@]} ; i++)); do MYARRAY=( ${MYARRAY[@]} ${SERVERS[$i]} ${DESCRIPTION[$i]} ) done Xdialog --menubox Choose 20 100 1 ${MYARRAY[@]}
Xdialog seems to take parameters in a very different format from what your post has. First, the dimensions should be separate parameters, not "300x160". Also, if you enclose a variable reference in single quotes as you have done, bash should not expand it.
NOTE: I would be surprised if there wasn't some simpler/handier way to 'interleave' arrays in bash that could be used instead of this for loop. But this should get the job done.
Thanks a lot, works like a charm.
Offline