You are not logged in.

#1 2012-03-22 23:01:30

poorguy
Member
Registered: 2010-01-24
Posts: 26

need help using arrays with Xdialog

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

#2 2012-03-23 00:19:59

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,330
Website

Re: need help using arrays with Xdialog

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

#3 2012-03-23 05:01:56

livibetter
Member
From: Taipei
Registered: 2008-05-14
Posts: 95
Website

Re: need help using arrays with Xdialog

Trilby wrote:

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

#4 2012-03-24 11:42:50

poorguy
Member
Registered: 2010-01-24
Posts: 26

Re: need help using arrays with Xdialog

Trilby wrote:

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

Board footer

Powered by FluxBB