Script variables values lost... [SOLVED]

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

Script variables values lost... [SOLVED]

#1 Post by Argolance »

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. :cry:

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.

User avatar
GustavoYz
Posts: 883
Joined: Wed 07 Jul 2010, 05:11
Location: .ar

#2 Post by GustavoYz »

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

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

#3 Post by Moose On The Loose »

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
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
Since gtkdialog outputs stuff that has the "THING=VALUE" form just saving that to a file is good enough in some cases.

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#4 Post by Argolance »

Bonjour,
Thank you for answering...
GustavoYz wrote:Hi, Im not sure of understand the question fully...
I made this little script so that things are more understandable:

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
I tried this too:

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
This is the content of $HOME/parameters:

Code: Select all

CB1=true
CB2=true
CB3=true
Hope this is clearer!
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 
I don't really understand your code lines but they seem to be the equivalent of the last part of the second script above?. :oops:

Cordialement.

User avatar
SFR
Posts: 1800
Joined: Wed 26 Oct 2011, 21:52

#5 Post by SFR »

Hey Argolance

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> 
or less ambiguous:

Code: Select all

            <variable>output_CB1</variable>
            <default>$CB1</default>
Works for me. 8)

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]

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#6 Post by Argolance »

Hello SFR,
SFR wrote:Hmm, I've never seen that "active" attribute before.
Found in gtkdialog4 examples (/usr/share/doc/gtkdialog4/examples/checkbox/checkbox_attributes):

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>
[...]
I replaced <checkbox active="$CB1"> with <default>$CB1</default> and yes, indeed, it works... :D

Thank you a lot.

NOTE: Maybe this should be told on gtkdialog tips, because it doesn't seem to be normal?

Cordialement.

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

#7 Post by Moose On The Loose »

Argolance wrote:Bonjour,
*** snip down to only my script fragment and the comment ***
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 
I don't really understand your code lines but they seem to be the equivalent of the last part of the second script above?. :oops:
Cordialement.
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.

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#8 Post by Argolance »

Bonsoir,
@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
Thank you!

Cordialement.

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#9 Post by don570 »

For the record...

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
if you have a button that saves the variable contents.
For example this would save CHECKBOX1's contents

Code: Select all

<button ok>
<action>echo $CHECKBOX1  > /tmp/checkbox.tmp</action>
</button>
______________________________________________

User avatar
Argolance
Posts: 3767
Joined: Sun 06 Jan 2008, 22:57
Location: PORT-BRILLET (Mayenne - France)
Contact:

#10 Post by Argolance »

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:

Code: Select all

 I=$IFS; IFS=""
for STATEMENTS in  $(gtkdialog --program=test --center); do
    eval $STATEMENTS
done
IFS=$I
This seems to be somewhat strange...

Cordialement.

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#11 Post by don570 »

I didn't read the first post very carefully :oops:

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

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

#12 Post by Moose On The Loose »

Argolance wrote:Bonsoir,
@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
Thank you!

Cordialement.
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.

Post Reply