Re: gtkdialog – variables

Johnny Rosenberg gurus.knugum at gmail.com
Wed Apr 17 19:08:01 UTC 2013


2013/4/17 Johnny Rosenberg <gurus.knugum at gmail.com>:
> 2013/4/17 Johnny Rosenberg <gurus.knugum at gmail.com>:
>> I'm experimenting a bit (at level ”beginner”) with gtkdialog and there
>> is one thing that I can't figure out.
>>
>> I can use variables in dialogues, but I can't use those variables
>> ”outside” the dialogue in my script.
>>
>> Here's an example, in which the user pick a car and a colour. Of
>> course I want to set two variables with the user's choice.
>>
>> First create two text files, one of them with car brands and the other
>> one with colours:
>> Cars:
>> Citroën
>> Mazda
>> Škoda
>> Volvo
>>
>> Colours:
>> Black
>> Brown
>> Red
>> Orange
>> Yellow
>> Green
>> Blue
>> Violette
>> Gray
>> White
>>
>> Now the script:
>> #!/bin/bash
>> export MainDialog='
>>     <vbox>
>>         <hbox>
>>             <text>
>>                 <label>Car:</label>
>>             </text>
>>             <comboboxentry>
>>                 <default>Select a bloody car</default>
>>                 <variable>SelectedCar</variable>
>>                 <input file>Cars</input>
>>             <sensitive>true</sensitive>
>>             </comboboxentry>
>>             <text>
>>                 <label>Colour:</label>
>>             </text>
>>             <comboboxentry>
>>                 <default>Are you going to select a colour or what?</default>
>>                 <variable>SelectedColour</variable>
>>                 <input file>Colours</input>
>>             </comboboxentry>
>>         </hbox>
>>         <hbox>
>>             <button ok></button>
>>             <button cancel></button>
>>         </hbox>
>>     </vbox>'
>> gtkdialog --program=MainDialog
>> # ----------------------------------------------------------------------------------------------
>>
>> Running this, selecting ”Citroën” and ”Black” and hitting OK gives the
>> following output (at standard out):
>> SelectedCar="Citroën"
>> SelectedColour="Black"
>> EXIT="OK"
>>
>> However, when trying to use those variables in the Bash part of the
>> script, they do no longer exist:
>> #!/bin/bash
>> export MainDialog='
>>     <vbox>
>>         <hbox>
>>             <text>
>>                 <label>Car:</label>
>>             </text>
>>             <comboboxentry>
>>                 <default>Select a bloody car</default>
>>                 <variable>SelectedCar</variable>
>>                 <input file>Cars</input>
>>             <sensitive>true</sensitive>
>>             </comboboxentry>
>>             <text>
>>                 <label>Colour:</label>
>>             </text>
>>             <comboboxentry>
>>                 <default>Are you going to select a colour or what?</default>
>>                 <variable>SelectedColour</variable>
>>                 <input file>Colours</input>
>>             </comboboxentry>
>>         </hbox>
>>         <hbox>
>>             <button ok></button>
>>             <button cancel></button>
>>         </hbox>
>>     </vbox>'
>> gtkdialog --program=MainDialog
>>
>> echo "Buy a ${SelectedColour} ${SelectedCar} today."
>> echo "Tomorrow it may be illegal."
>> # ----------------------------------------------------------------------------------------------
>>
>> Both of those variables are empty, unfortunately.
>>
>> I read as much of the documentation as I found about this, and I only
>> found this information:
>>
>> Directives
>> Name: variable export="false"      Description: Variable name, not
>> exported to shell
>>
>> Therefore I tried:
>> <variable export="true">SelectedCar</variable>
>>
>> However, with no luck at all.
>>
>> Any ideas, please?
>>
>>
>> Ubuntu 12.04
>> gtkdialog 0.8.3, compiled from source, see
>> http://code.google.com/p/gtkdialog/downloads/detail?name=gtkdialog-0.8.3.tar.gz&can=2&q=
>> That page also says: ”Added support for <variable
>> export="false">VARNAME</variable>”
>> That doesn't seem to be necessary, since the variable doesn't seem to
>> be exported anyway…
>>
>>
>> Johnny Rosenberg
>
> I just found an example script with variables that actually works:
> #! /bin/bash
>
> export DIALOG='
>     <vbox>
>        <entry>
>            <variable>ENTRY</variable>
>        </entry>
>     <hbox>
>         <button ok></button>
>         <button cancel></button>
>     </hbox>
> </vbox>'
>
> I=$IFS; IFS=""
> for STATEMENTS in $(gtkdialog --program DIALOG); do
>     eval $STATEMENTS
> done
> IFS=$I
>
> if [ "$EXIT" = "OK" ]; then
>     echo "You entered: $ENTRY."
> else
>     echo "You pressed the Cancel button."
> fi
>
> Seems like they use ”eval” to set the variables. That is of course one
> way to do it, but should I really need to do that? If so, when is
> ”<variable export="false">VariableName</variable>” necessary?
>
> Johnny Rosenberg

Okay, did some further experiments. Seems like I just misunderstood
the whole thing. The only way to use the user entries is to grab what
is sent to the standard output from the dialogue. That
”export="false"” thing prevents a particular variable to be sent to
the standard output.

I am not sure that is a good thing though, but at least I learned
something today…


Johnny Rosenberg




More information about the ubuntu-users mailing list