Script variables values lost... [SOLVED]
- Argolance
- Posts: 3767
- Joined: Sun 06 Jan 2008, 22:57
- Location: PORT-BRILLET (Mayenne - France)
- Contact:
Script variables values lost... [SOLVED]
Bonjour,
Not very simple to explain...
So, I created a script with several tabs containing (by categories) check boxes and other widgets to be set by user. I just have a single "Apply" button which intend to save all parameters at the same time.
But parameters that are on tabs user doesn't click on (because he doesn't need to change anything here) are not taken into account while applying and when script is launched again, all these parameters (default or previously set by user) are lost.
To sum up: If I put all parameters on the same tab, there is no problem but when dispatched on several tabs, it doesn't work anymore, unless user clicks on every tab before applying.
Is it clear?
How could this be solved?
Thanks for your attention.
Cordialement.
Not very simple to explain...
So, I created a script with several tabs containing (by categories) check boxes and other widgets to be set by user. I just have a single "Apply" button which intend to save all parameters at the same time.
But parameters that are on tabs user doesn't click on (because he doesn't need to change anything here) are not taken into account while applying and when script is launched again, all these parameters (default or previously set by user) are lost.
To sum up: If I put all parameters on the same tab, there is no problem but when dispatched on several tabs, it doesn't work anymore, unless user clicks on every tab before applying.
Is it clear?
How could this be solved?
Thanks for your attention.
Cordialement.
Last edited by Argolance on Wed 23 Oct 2013, 17:29, edited 1 time in total.
Hi, Im not sure of understand the question fully...
However, I think that as long as is possible to write down those
settings/options into a text file (by the user or the script itself), you can always "source" it within your script:
However, I think that as long as is possible to write down those
settings/options into a text file (by the user or the script itself), you can always "source" it within your script:
Code: Select all
#Testing - text file
A=1
B=2
C="Hi, this is a test!"
Code: Select all
#!/usr/bin/bash
source /some/file.txt
echo $A $B $C
exit 0
- Moose On The Loose
- Posts: 965
- Joined: Thu 24 Feb 2011, 14:54
I do something a lot like that. If you are using gtkdialog3, you can get it a bit better with something like this:GustavoYz wrote:Hi, Im not sure of understand the question fully...
However, I think that as long as is possible to write down those
settings/options into a text file (by the user or the script itself), you can always "source" it within your script:Code: Select all
#Testing - text file A=1 B=2 C="Hi, this is a test!"
Code: Select all
#!/usr/bin/bash source /some/file.txt echo $A $B $C exit 0
Code: Select all
RESULT=$( gtkdialog3 options )
if ( echo "$RESULT" | grep -q "=Apply" ) ; then
echo "$RESULT" >$RCFILE
eval "$RESULT"
... more stuff ...
fi
- Argolance
- Posts: 3767
- Joined: Sun 06 Jan 2008, 22:57
- Location: PORT-BRILLET (Mayenne - France)
- Contact:
Bonjour,
Thank you for answering...
I tried this too:
This is the content of $HOME/parameters:
Hope this is clearer!
Cordialement.
Thank you for answering...
I made this little script so that things are more understandable:GustavoYz wrote:Hi, Im not sure of understand the question fully...
Code: Select all
#!/bin/sh
. $HOME/parameters
apply(){
echo "CB1=$CB1" > $HOME/parameters
echo "CB2=$CB2" >> $HOME/parameters
echo "CB3=$CB3" >> $HOME/parameters
}
export -f apply
export test="
<window title="TEST">
<vbox>
<notebook labels="Tab 1 | Tab 2 | Tab 3">
<vbox>
<checkbox active="$CB1">
<label>Parameter 1</label>
<variable>CB1</variable>
</checkbox>
</vbox>
<vbox>
<checkbox active="$CB2">
<label>Parameter 2</label>
<variable>CB2</variable>
</checkbox>
</vbox>
<vbox>
<checkbox active="$CB3">
<label>Parameter 3</label>
<variable>CB3</variable>
</checkbox>
</vbox>
</notebook>
<hbox>
<button><input file stock="gtk-apply"></input>
<action>apply</action>
<action>exit:ok</action>
</button>
</hbox>
</vbox>
</window>"
I=$IFS; IFS=""
for STATEMENTS in $(gtkdialog --program=test --center); do
eval $STATEMENTS
done
IFS=$I
Code: Select all
#!/bin/sh
. $HOME/parameters
apply(){
echo "CB1=$CB1" > $HOME/parameters
echo "CB2=$CB2" >> $HOME/parameters
echo "CB3=$CB3" >> $HOME/parameters
}
export -f apply
export test="
<window title="TEST">
<vbox>
<notebook labels="Tab 1 | Tab 2 | Tab 3">
<vbox>
<checkbox active="$CB1">
<label>Parameter 1</label>
<variable>CB1</variable>
</checkbox>
</vbox>
<vbox>
<checkbox active="$CB2">
<label>Parameter 2</label>
<variable>CB2</variable>
</checkbox>
</vbox>
<vbox>
<checkbox active="$CB3">
<label>Parameter 3</label>
<variable>CB3</variable>
</checkbox>
</vbox>
</notebook>
<hbox>
<button><input file stock="gtk-apply"></input>
<action>apply</action>
<action>EXIT:doapply</action>
</button>
</hbox>
</vbox>
</window>"
I=$IFS; IFS=""
for STATEMENTS in $(gtkdialog --program=test --center); do
eval $STATEMENTS
done
IFS=$I
if [ "$EXIT" = "doapply" ]; then
echo "CB1=$CB1" > $HOME/parameters
echo "CB2=$CB2" >> $HOME/parameters
echo "CB3=$CB3" >> $HOME/parameters
fi
Code: Select all
CB1=true
CB2=true
CB3=true
I don't really understand your code lines but they seem to be the equivalent of the last part of the second script above?.Moose On The Loose wrote:I do something a lot like that. If you are using gtkdialog3, you can get it a bit better with something like this:Code: Select all
RESULT=$( gtkdialog3 options ) if ( echo "$RESULT" | grep -q "=Apply" ) ; then echo "$RESULT" >$RCFILE eval "$RESULT" ... more stuff ... fi
Cordialement.
Hey Argolance
Hmm, I've never seen that "active" attribute before.
I always use <default> tag instead:
or less ambiguous:
Works for me.
HTH
Greetings!
Hmm, I've never seen that "active" attribute before.
I always use <default> tag instead:
Code: Select all
<checkbox>
<label>Parameter 1</label>
<variable>CB1</variable>
<default>$CB1</default>
</checkbox>
Code: Select all
<variable>output_CB1</variable>
<default>$CB1</default>
HTH
Greetings!
[color=red][size=75][O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource[/size][/color]
[b][color=green]Omnia mea mecum porto.[/color][/b]
[b][color=green]Omnia mea mecum porto.[/color][/b]
- Argolance
- Posts: 3767
- Joined: Sun 06 Jan 2008, 22:57
- Location: PORT-BRILLET (Mayenne - France)
- Contact:
Hello SFR,
I replaced <checkbox active="$CB1"> with <default>$CB1</default> and yes, indeed, it works...
Thank you a lot.
NOTE: Maybe this should be told on gtkdialog tips, because it doesn't seem to be normal?
Cordialement.
Found in gtkdialog4 examples (/usr/share/doc/gtkdialog4/examples/checkbox/checkbox_attributes):SFR wrote:Hmm, I've never seen that "active" attribute before.
Code: Select all
export MAIN_DIALOG='
<vbox>
<frame Checkbox attributes example>
<checkbox active="true">
<label>Checkbox with active set to true</label>
<variable>CHECKBOX1</variable>
<action>echo Checkbox is $CHECKBOX1 now.</action>
</checkbox>
[...]
Thank you a lot.
NOTE: Maybe this should be told on gtkdialog tips, because it doesn't seem to be normal?
Cordialement.
- Moose On The Loose
- Posts: 965
- Joined: Thu 24 Feb 2011, 14:54
The point I was making is that gtkdialog produces an output that looks just like assignment to variables and as a result, you can just save that text for later and use "eval" to make it do the actual assignments now. Thus in many cases, you can use very few lines to get the effect desired. The settings from this time can be remembered to become the defaults for next time and then used this time.Argolance wrote:Bonjour,
*** snip down to only my script fragment and the comment ***I don't really understand your code lines but they seem to be the equivalent of the last part of the second script above?.Moose On The Loose wrote:I do something a lot like that. If you are using gtkdialog3, you can get it a bit better with something like this:Code: Select all
RESULT=$( gtkdialog3 options ) if ( echo "$RESULT" | grep -q "=Apply" ) ; then echo "$RESULT" >$RCFILE eval "$RESULT" ... more stuff ... fi
Cordialement.
- Argolance
- Posts: 3767
- Joined: Sun 06 Jan 2008, 22:57
- Location: PORT-BRILLET (Mayenne - France)
- Contact:
Bonsoir,
@Moose On The Loose
It is exactly what these lines (above) do...
Thank you!
Cordialement.
@Moose On The Loose
It is exactly what these lines (above) do...
Code: Select all
I=$IFS; IFS=""
for STATEMENTS in $(gtkdialog --program=test --center); do
eval $STATEMENTS
done
IFS=$I
if [ "$EXIT" = "doapply" ]; then
echo "CB1=$CB1" > $HOME/parameters
echo "CB2=$CB2" >> $HOME/parameters
echo "CB3=$CB3" >> $HOME/parameters
fi
Cordialement.
For the record...
You can avoid using for statement
if you have a button that saves the variable contents.
For example this would save CHECKBOX1's contents
______________________________________________
You can avoid using for statement
Code: Select all
I=$IFS; IFS=""
for STATEMENTS in $(gtkdialog --program=test --center); do
eval $STATEMENTS
done
IFS=$I
For example this would save CHECKBOX1's contents
Code: Select all
<button ok>
<action>echo $CHECKBOX1 > /tmp/checkbox.tmp</action>
</button>
- Argolance
- Posts: 3767
- Joined: Sun 06 Jan 2008, 22:57
- Location: PORT-BRILLET (Mayenne - France)
- Contact:
Bonsoir,
@don570
As said above, the problem was solved replacing <checkbox active=\"$VAR\"> (code lines found in gtkdialog4 examples - /usr/share/doc/gtkdialog4/examples/checkbox/checkbox_attributes) with <default>$VAR</default>.
I sent you a PM just for you to pay attention to the fact that <checkbox active=\"$VAR\"> doesn't take variables into account when check boxes are on several tabs, even using:
This seems to be somewhat strange...
Cordialement.
@don570
As said above, the problem was solved replacing <checkbox active=\"$VAR\"> (code lines found in gtkdialog4 examples - /usr/share/doc/gtkdialog4/examples/checkbox/checkbox_attributes) with <default>$VAR</default>.
I sent you a PM just for you to pay attention to the fact that <checkbox active=\"$VAR\"> doesn't take variables into account when check boxes are on several tabs, even using:
Code: Select all
I=$IFS; IFS=""
for STATEMENTS in $(gtkdialog --program=test --center); do
eval $STATEMENTS
done
IFS=$I
Cordialement.
I didn't read the first post very carefully
I've written a page in my Gtkdialog Tutorial Manual to explain the
checkbox widget
http://murga-linux.com/puppy/viewtopic.php?t=89045
I discovered that bash shell needs to be used if parameters.tmp file
doesn't exist.
Here is the example I use to explain checkbox widget.
I advise to not use use the notebook widget unless all the checkboxes
are in one tab.
Also I am consistent in using strong quotes for GTKDIALOG variable.
___________________________________________________
I've written a page in my Gtkdialog Tutorial Manual to explain the
checkbox widget
http://murga-linux.com/puppy/viewtopic.php?t=89045
I discovered that bash shell needs to be used if parameters.tmp file
doesn't exist.
Here is the example I use to explain checkbox widget.
I advise to not use use the notebook widget unless all the checkboxes
are in one tab.
Also I am consistent in using strong quotes for GTKDIALOG variable.
___________________________________________________
Code: Select all
#!/bin/bash
. /tmp/parameters.tmp
apply(){
echo "CB1=$CB1" > /tmp/parameters.tmp
echo "CB2=$CB2" >> /tmp/parameters.tmp
echo "CB3=$CB3" >> /tmp/parameters.tmp
}
export -f apply
export MAIN_DIALOG='
<window title="TEST">
<vbox>
<vbox>
<checkbox active="'$CB1'">
<label>Parameter 1</label>
<variable>CB1</variable>
</checkbox>
</vbox>
<vbox>
<checkbox active="'$CB2'">
<label>Parameter 2</label>
<variable>CB2</variable>
</checkbox>
</vbox>
<vbox>
<checkbox active="'$CB3'">
<label>Parameter 3</label>
<variable>CB3</variable>
</checkbox>
</vbox>
<hbox>
<button>
<input file stock="gtk-apply"></input>
<action>apply</action>
<action>exit:ok</action>
</button>
</hbox>
</vbox>
</window>'
gtkdialog --program MAIN_DIALOG
- Moose On The Loose
- Posts: 965
- Joined: Thu 24 Feb 2011, 14:54
Notice that my version didn't do a for loop. I just did an eval on the whole collection in one go and stored the whole collection in a single write. My method doesn't allow any logic on a line by line basis but usually is what is needed.Argolance wrote:Bonsoir,
@Moose On The Loose
It is exactly what these lines (above) do...Thank you!Code: Select all
I=$IFS; IFS="" for STATEMENTS in $(gtkdialog --program=test --center); do eval $STATEMENTS done IFS=$I if [ "$EXIT" = "doapply" ]; then echo "CB1=$CB1" > $HOME/parameters echo "CB2=$CB2" >> $HOME/parameters echo "CB3=$CB3" >> $HOME/parameters fi
Cordialement.