Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Fri 19 Dec 2014, 23:06
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Script variables values lost... [SOLVED]
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [12 Posts]  
Author Message
Argolance


Joined: 06 Jan 2008
Posts: 1851
Location: PORT-BRILLET (Mayenne - France)

PostPosted: Tue 22 Oct 2013, 10:57    Post subject:  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. Crying or Very sad

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, 13:29; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website 
GustavoYz


Joined: 07 Jul 2010
Posts: 897
Location: .ar

PostPosted: Tue 22 Oct 2013, 18:27    Post subject:  

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:
#Testing - text file
A=1
B=2
C="Hi, this is a test!"

Code:
#!/usr/bin/bash
source /some/file.txt
echo $A $B $C
exit 0

_________________

Back to top
View user's profile Send private message 
Moose On The Loose


Joined: 24 Feb 2011
Posts: 544

PostPosted: Tue 22 Oct 2013, 22:14    Post subject:  

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:
#Testing - text file
A=1
B=2
C="Hi, this is a test!"

Code:
#!/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:

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.
Back to top
View user's profile Send private message 
Argolance


Joined: 06 Jan 2008
Posts: 1851
Location: PORT-BRILLET (Mayenne - France)

PostPosted: Wed 23 Oct 2013, 11:03    Post subject:  

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:
#!/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:
#!/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:
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:
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?. Embarassed

Cordialement.

_________________

Back to top
View user's profile Send private message Visit poster's website 
SFR


Joined: 26 Oct 2011
Posts: 1111

PostPosted: Wed 23 Oct 2013, 11:31    Post subject:  

Hey Argolance

Hmm, I've never seen that "active" attribute before.
I always use <default> tag instead:
Code:
         <checkbox>
            <label>Parameter 1</label>
            <variable>CB1</variable>
            <default>$CB1</default>
         </checkbox>

or less ambiguous:
Code:
            <variable>output_CB1</variable>
            <default>$CB1</default>

Works for me. Cool

HTH
Greetings!

_________________
[O]bdurate [R]ules [D]estroy [E]nthusiastic [R]ebels => [C]reative [H]umans [A]lways [O]pen [S]ource
Omnia mea mecum porto.
Back to top
View user's profile Send private message 
Argolance


Joined: 06 Jan 2008
Posts: 1851
Location: PORT-BRILLET (Mayenne - France)

PostPosted: Wed 23 Oct 2013, 13:28    Post subject:  

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:
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... Very Happy

Thank you a lot.

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

Cordialement.

_________________

Back to top
View user's profile Send private message Visit poster's website 
Moose On The Loose


Joined: 24 Feb 2011
Posts: 544

PostPosted: Thu 24 Oct 2013, 10:37    Post subject:  

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:
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?. Embarassed
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.
Back to top
View user's profile Send private message 
Argolance


Joined: 06 Jan 2008
Posts: 1851
Location: PORT-BRILLET (Mayenne - France)

PostPosted: Thu 24 Oct 2013, 14:02    Post subject:  

Bonsoir,
@Moose On The Loose
It is exactly what these lines (above) do...
Code:
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.

_________________

Back to top
View user's profile Send private message Visit poster's website 
don570


Joined: 10 Mar 2010
Posts: 3437
Location: Ontario

PostPosted: Thu 24 Oct 2013, 19:30    Post subject:  

For the record...

You can avoid using for statement
Code:
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:
<button ok>
<action>echo $CHECKBOX1  > /tmp/checkbox.tmp</action>
</button>

______________________________________________
Back to top
View user's profile Send private message 
Argolance


Joined: 06 Jan 2008
Posts: 1851
Location: PORT-BRILLET (Mayenne - France)

PostPosted: Fri 25 Oct 2013, 12:00    Post subject:  

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:
 I=$IFS; IFS=""
for STATEMENTS in  $(gtkdialog --program=test --center); do
    eval $STATEMENTS
done
IFS=$I

This seems to be somewhat strange...

Cordialement.

_________________

Back to top
View user's profile Send private message Visit poster's website 
don570


Joined: 10 Mar 2010
Posts: 3437
Location: Ontario

PostPosted: Mon 28 Oct 2013, 19:48    Post subject:  

I didn't read the first post very carefully Embarassed

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:

#!/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
Back to top
View user's profile Send private message 
Moose On The Loose


Joined: 24 Feb 2011
Posts: 544

PostPosted: Mon 28 Oct 2013, 21:45    Post subject:  

Argolance wrote:
Bonsoir,
@Moose On The Loose
It is exactly what these lines (above) do...
Code:
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.
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [12 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.1302s ][ Queries: 11 (0.0156s) ][ GZIP on ]