You are not logged in.

#1 2020-01-04 13:40:55

KairiTech
Member
From: Toronto, Canada
Registered: 2011-06-04
Posts: 275

[SOLVED] Zenity and array variables

SYNOPSES: pipe the list variable to Zenity instead of populating and passing an array:

I create an array containg a list of pacman packages/descriptions with the below code:

installedPKGs=()
for PKG in zsh conky ; do
   installedPKGs=("${installedPKGs[@]}FALSE ${PKG} \"$(pacman -Qi ${PKG} | egrep 'Description' | cut -c19-99)\" ")
done

the array then contains:

echo "${installedPKGs[@]}"

FALSE which "A utility to show the full path of commands" FALSE zsh "A very advanced and programmable command interpreter (shell) for UNIX" FALSE conky "Lightweight system monitor for X" FALSE pacman "A library-based package manager with dependency support"

using the above value of the array as a Zenity paramater as shown below works as expected:

selectedPKG=$( \
                zenity --width=900 --height=490 --list --radiolist --text "package to REMOVE" --column "select" --column "package" --column "description" \
                FALSE zsh "A very advanced and programmable command interpreter (shell) for UNIX" FALSE conky "Lightweight system monitor for X" \
             )

However, when I pass the array as a paramater as shown below it results in a blank selection list:

selectedPKG=$( zenity --width=900 --height=490 --list --radiolist --text "package to REMOVE" --column "select" --column "package" --column "description" "${installedPKGs[@]}" )

Am I building/passing the array to zenity incorrectly?

Last edited by KairiTech (2020-01-04 16:56:29)

Offline

#2 2020-01-04 14:54:06

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

Re: [SOLVED] Zenity and array variables

If (big if) you wanted to do this bit by bit like this and store all the content in a variable, you'd need to do a lot of gynmastics with an `eval` before the zenity command line to expand the variable before other processing of the command line.

But really this is much much simpler, don't loop and use dozens of subshells and additional processes creating variables - just pipe:

pkgs="zsh conky"

pacman -Qi $pkgs | \
   sed -n 's/^Name *: /FALSE\n/p;s/^Description *: //p' | \
   zenity --width=900 --height=490 --list --radiolist --text "package" --column "select" --column "package" --column "description"

Tally: your approach would use (at least) 3N+1 subshells and N calls to pacman (where N is the number of packages), while mine uses 3 subshells total (2 as written, but I assume you'll wrap this in $() to store the result) and only 1 call to pacman no matter how many packages are in your input list.

Last edited by Trilby (2020-01-04 15:15:16)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2020-01-04 16:11:07

KairiTech
Member
From: Toronto, Canada
Registered: 2011-06-04
Posts: 275

Re: [SOLVED] Zenity and array variables

Perfect! Exactly what I was looking for and I much prefer the efficiency of your method.

Howerer, even though It does return the selected package name as expected I get the below error:

(zenity:214751): GLib-WARNING **: 11:05:27.906: ../glib/glib/giounix.c:410Error while getting flags for FD: Bad file descriptor (9)

Offline

#4 2020-01-04 16:49:07

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

Re: [SOLVED] Zenity and array variables

I just had to install zenity to contribute to this thread - and I was getting that error regardless of the zenity command used - even if I used examples quoted in the man page.  Just redirect stderr to ignore it.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2020-01-04 16:52:23

KairiTech
Member
From: Toronto, Canada
Registered: 2011-06-04
Posts: 275

Re: [SOLVED] Zenity and array variables

Yep! Zenity is not perfect.

Thanks again for your help.

Offline

#6 2020-01-04 16:54:45

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

Re: [SOLVED] Zenity and array variables

It's actually a glib error, and I see reports of it going back a decade - but I could be missing something as I don't use any glib/gtk tools ... partially for reasons just like this (there are many cases of poorly handling file descriptors and streams in silly ways).  For present purposes, though, the message is harmless.

Last edited by Trilby (2020-01-04 16:55:18)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2020-01-04 16:57:32

KairiTech
Member
From: Toronto, Canada
Registered: 2011-06-04
Posts: 275

Re: [SOLVED] Zenity and array variables

Yep! Zenity is not perfect.

Thanks again for your help.

Offline

Board footer

Powered by FluxBB