[SOLVED] Reading lines to an array and generate dynamic zenity list
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Reading lines to an array and generate dynamic zenity list
Hi there,
first some explanation what I want to do exactly:
I have a textfile which looks for instance like this:
file.txt:
...
something=else to do
name=job one
name=job two
something=else again
whatever=dont know
name=gohome
...
I want to read all lines that starts with "name=" into an array. From this array a zenity-radiolist has to be generated. How often "name=" comes up in file.txt is variable/changes.
Regarding the upper example my zenity-radiolist shall have three choices:
job one
job two
gohome
What I've got so far:
Code:
#!/bin/bash
sfile="~/file.txt"
foo=( $(grep 'name=' $sfile))
# change IFS to linebrake to avoid problem with spaces
IFS=$'\n'
# I want to get rid off 'name=', I want the pure job-description
# this gives a propper output:
echo ${foo[0]} | awk 'BEGIN {FS="="} {print $2}'
echo ${foo[1]} | awk 'BEGIN {FS="="} {print $2}'
echo ${foo[3]} | awk 'BEGIN {FS="="} {print $2}'
# but how can i get rid of 'name=' permanently? How can I get my jobs into the array without the leading 'name='?
# anyway, next step would be to create the zenity-radiolist...
# I tried like this:
zenity --list --text="Show jobs:" --radiolist --column "Choice" --column "Job-name" \
TRUE ${foo[@]::1} $(printf 'FALSE %s\n' ${foo[@]::1})
From my point of view the zenity-radiolist should display the first item as checked and any other item as unchecked. It's a pity but the list shows only the first item (which indeed is checked) and no other items. Why?
So, my questions are:
1) How can I get rid of 'name=' from all items in my array or transfer the items without the leading 'name=' to a new array?
2) What is wrong with my zenity-radiolist? Why doesn't it display all items which are in my array?
1) (not tested) foo=( $(grep 'name=' $sfile | sed 's/name=//' ))
2) ${foo[@]::1} may not be valid. Try replacing TRUE ${foo[@]::1} with TRUE ${foo[1]} and $(printf 'FALSE %s\n' ${foo[@]::1}) with FALSE ${foo[2]}
which as a script takes the file name containing the option list as an argument.
By default, the first job is selected.
Replace the "$@" with the job list file name, if you incorporate it into your own scripts.
Personally, I'd prefer to allow the user to select any jobs, with all selected by default:
Code:
#!/bin/bash
OLDIFS="$IFS"
IFS="
"
choices=()
for name in `sed -ne 's|^[Nn][Aa][Mm][Ee][\t ]*=[\t ]*\(.*[^\t ]\)[\t ]*$|\1|p' "$@"` ; do
choices=("${choices[@]}" true "$name")
done
choices=(`zenity \
--list \
--separator="$IFS" \
--checklist --multiple \
--text="Show jobs:" \
--column "Run" \
--column "Job name" \
"${choices[@]}"`)
IFS="$OLDIFS"
echo "You selected ${#choices[@]}: "
for choice in "${choices[@]}" ; do
echo -e "\t'$choice'"
done
For me (zenity 2.32.0 and bash 4.1.5) the above work fine with your input.
Last edited by Nominal Animal; 05-18-2011 at 04:26 PM.
I have another question regarding this thread, therefore I marked this thread as "unsolved", again.
The list works perfect, once more thanks a lot. I played around with the radiolist as well as with the multiple checklist (see Nominal Animal's posting).
But now I have another question:
What if I want to add the correct status to a checklist?
Again I start with my file.txt:
...
something=else to do
name=job one
name=job two
something=else again
whatever=dont know
name=gohome
...
The code of Nominal Animal (see code below) allows to generate the perfect checklist and a selection has been done:
Code:
#!/bin/bash
OLDIFS="$IFS"
IFS="
"
choices=()
for name in `sed -ne 's|^[Nn][Aa][Mm][Ee][\t ]*=[\t ]*\(.*[^\t ]\)[\t ]*$|\1|p' "$@"` ; do
choices=("${choices[@]}" true "$name")
done
choices=(`zenity \
--list \
--separator="$IFS" \
--checklist --multiple \
--text="Show jobs:" \
--column "Run" \
--column "Job name" \
"${choices[@]}"`)
IFS="$OLDIFS"
echo "You selected ${#choices[@]}: "
for choice in "${choices[@]}" ; do
echo -e "\t'$choice'"
done
What if I want to assign a status (TRUE/FALSE) to one or multiple of my jobs, so they are already checked or just not (regarding their current status)?
Let's say after selecting my first checklist I got a new file results.txt which looks like this:
job one=TRUE
job two=TRUE
gohome=FALSE
I thought about it but I have no clue how to get this realized?
Does anybody know & can advice how a checklist for results.txt can be coded that shows the correct status? Number of items in results.txt can vary, so the checklist needs to be dynamic, again.
Regards,
jomann.
Last edited by jomann; 05-24-2011 at 09:22 AM.
Reason: edit misspelling.
thanks a lot for your answer. I already thought about changing file.txt, too. But I get the file.txt from some other program on which I have no influance how it stores the values in file.txt. I could add TRUE/FALSE to the file somehow in dependency to some other info, but as soon as somebody works with the other program the file.txt is going to change (maybe, maybe not).
Hm... any other idea?
Can anybody make clear whether my idea with a loop in a loop would fit for my problem?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.