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 Sun 26 May 2013, 04:59
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Puzzled by variable evaluation problem. [ Solved ]
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [9 Posts]  
Author Message
sunburnt


Joined: 08 Jun 2005
Posts: 4006
Location: Arizona, U.S.A.

PostPosted: Wed 12 Oct 2011, 04:44    Post subject:  Puzzled by variable evaluation problem. [ Solved ]  

This code echoes the variable name and not it`s contents:
Code:
#!/usr/bin/bash
gtk-server -ipc=$$ -log=/tmp/$0.log &
sleep 1

CMDs='
gtk_init "NULL NULL"
winMAIN gtk_window_new 0
gtk_window_set_title $winMAIN "Win._Title"
gtk_window_set_position $winMAIN 1
tblMAIN gtk_table_new 10 10 1
gtk_container_add $winMAIN $tblMAIN'

ipc() { eval $1=`gtk-server msg=$$,"$2 $3 $4 $5 $6 $7"`; }

echo "$CMDs" |while read CMD
do
   [ ! "$CMD" ]&& continue
   [ "`echo "$CMD" |grep '^#'`" ]&& continue

   if [ "`echo $CMD |grep '^gtk'`" ];then
echo -e '\n### CMD ###   '"$CMD"
      ID=`gtk-server msg=$$,"$CMD"`
echo '### RET = '"$ID"
   else
echo -e '\n### NEW ###   '"$CMD"
      ipc $CMD
echo '### $winMAIN = '$winMAIN'     ### $tblMAIN = '$tblMAIN
   fi
done

I think it errors ( assertion `GTK_IS_WINDOW (window)' failed ) because
the variables are treated as literals ( the output lines: ### CMD ### .......).
The last echo line shows the 2 variables have the values in them.
It seems the variables don`t evaluate to their values and gtk-server errors.

Also it can`t make the log file ( I can`t see the problem there...):
Error: ( WARNING: The logfile could not be created. )

Last edited by sunburnt on Thu 13 Oct 2011, 03:53; edited 1 time in total
Back to top
View user's profile Send private message 
amigo

Joined: 02 Apr 2007
Posts: 1759

PostPosted: Wed 12 Oct 2011, 05:26    Post subject:  

Double-quotes:
CMDs=" ... "

gtk-server -ipc=$$ -log=/tmp/$0.log
Again, try double quoting:
gtk-server -ipc=$$ -log="/tmp/$0.log"
although it may be refusing because the file already exists. This:
gtk-server -ipc=$$ -log="/tmp/$$.log"
will give you a unique name for the log eacg time you run the command.
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 4006
Location: Arizona, U.S.A.

PostPosted: Wed 12 Oct 2011, 06:05    Post subject:  

Thanks amigo; I`ve tried that, it displays this:
Code:
# gtkSrv_ipc.init
WARNING: The logfile could not be created.
/root/my-applications/sbin/gtkSrv_ipc.init: line 30: NULL
winMAIN gtk_window_new 0
gtk_window_set_title  Win._Title
gtk_widget_set_size_request  400 200
gtk_window_set_position  1
tblMAIN gtk_table_new 10 10 1
gtk_container_add  : command not found

Different errors... The property lines don`t have the variable values at all.
And the echo ### lines don`t show at all either!

"CMD=" part has the "NULL NULL" ( and others ) not in the double quotes.
I escaped the \" and the property lines still don`t have the variable values.
But the echo ### lines showed normally.

I`ve tried using "eval" to get the values out of the variables, but no luck...
# eval does work here: A='aaa' ; eval '$A'
I tried "expr" and it works in tests, but not in my code ( return = -1 ).

Also I tried: gtk-server -ipc=$$ -log="/tmp/$0.$$.log" &
Same error... And the file does not previously exist.

### Does the above code (file) have the same problems for you???
Back to top
View user's profile Send private message 
mcewanw

Joined: 16 Aug 2007
Posts: 1482
Location: New Zealand

PostPosted: Wed 12 Oct 2011, 18:43    Post subject:  

@sunburnt:

I haven't checked your whole program but the following will give you your logfile:

Code:

logfile="/tmp/$(basename "$0")"
gtk-server -ipc=$$ -log="$logfile" &

_________________
Non enim propter gloriam, diuicias aut honores pugnamus set propter libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.

Last edited by mcewanw on Wed 12 Oct 2011, 23:05; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website 
mcewanw

Joined: 16 Aug 2007
Posts: 1482
Location: New Zealand

PostPosted: Wed 12 Oct 2011, 20:31    Post subject:  

@sunburnt:

Bash won't expand a variable inside a variable directly. I think the following contains a solution for that. But another issue needs to be addressed still: I think the "Win._Title" string maybe needs to be passed to gtk-server inside quotes. The existing quotes are stripped during the "read" I think. I don't have more time to look into that further just now sorry:

Code:

#!/usr/bin/bash
logfile="/tmp/$(basename "$0")"
gtk-server -ipc=$$ -log="$logfile" &
sleep 1

CMDs='
gtk_init "NULL NULL"
winMAIN gtk_window_new 0
gtk_window_set_title $winMAIN "Win._Title"
gtk_window_set_position $winMAIN 1
tblMAIN gtk_table_new 10 10 1
gtk_container_add $winMAIN $tblMAIN'

ipc() { eval $1=`gtk-server msg=$$,"$2 $3 $4 $5 $6 $7"`;  }

echo "$CMDs" |while read CMD
do
   [ ! "$CMD" ]&& continue
   [ "`echo "$CMD" |grep '^#'`" ]&& continue

   if [ "`echo $CMD |grep '^gtk'`" ];then
echo -e '\n### CMD ###   '$(eval "echo \"$CMD\"")
      ID=`gtk-server msg=$$,$(eval "echo \"$CMD\"")`
echo '### RET = '"$ID"
   else
echo -e '\n### NEW ###   '"$CMD"
      ipc $(eval "echo \"$CMD\"")
echo '### $winMAIN = '$winMAIN'     ### $tblMAIN = '$tblMAIN
   fi
done

_________________
Non enim propter gloriam, diuicias aut honores pugnamus set propter libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.

Last edited by mcewanw on Wed 12 Oct 2011, 23:04; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website 
mcewanw

Joined: 16 Aug 2007
Posts: 1482
Location: New Zealand

PostPosted: Wed 12 Oct 2011, 21:28    Post subject:  

The following gives RET=ok for all the commands, so maybe helps you with your program's development, even though not at stage of displaying gui yet:

Code:

#!/usr/bin/bash
logfile="/tmp/$(basename "$0")"
gtk-server -ipc=$$ -log="$logfile" &
sleep 1

CMDs='
gtk_init "NULL NULL"
winMAIN gtk_window_new 0
gtk_window_set_title $winMAIN
gtk_window_set_position $winMAIN 1
tblMAIN gtk_table_new 10 10 1
gtk_container_add $winMAIN $tblMAIN'


ipc() { eval $1=`gtk-server msg=$$,"$2 $3 $4 $5 $6 $7"`; }

echo "$CMDs" |while read CMD
do
   [ ! "$CMD" ]&& continue
   [ "`echo "$CMD" |grep '^#'`" ]&& continue

   if [ "`echo $CMD |grep '^gtk'`" ];then
echo -e '\n### CMD ###   '$(eval "echo \"$CMD\"")

     if [ "`echo $CMD |grep 'set_title'`" ];then
       ID=`gtk-server msg=$$,"$(eval "echo \"$CMD\"") 'Just a test title'"`
     else
      ID=`gtk-server msg=$$,"$(eval "echo \"$CMD\"") 1"`
     fi
echo '### RET = '"$ID"
   else
echo -e '\n### NEW ###   '"$CMD"
      ipc $(eval "echo \"$CMD\"")
echo '### $winMAIN = '$winMAIN'     ### $tblMAIN = '$tblMAIN
   fi
done

_________________
Non enim propter gloriam, diuicias aut honores pugnamus set propter libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.
Back to top
View user's profile Send private message Visit poster's website 
mcewanw

Joined: 16 Aug 2007
Posts: 1482
Location: New Zealand

PostPosted: Wed 12 Oct 2011, 22:35    Post subject:  

@sunburnt:

EDIT: Added gtk_server_exit line.

The following modifications to your code display a gui in the manner you seem to be working towards. It's just proof of concept testing, NOT a complete program. You'll still need to work on the Title "quotes problem" etc, which I referred to earlier:

Code:

#!/usr/bin/bash
# sunburnt "proof of concept" code
# Revised/tested by mcewanw 13 Oct 2011

logfile="/tmp/$(basename "$0")"
gtk-server -ipc=$$ -log="$logfile" -detach
sleep 1

CMDs='
gtk_init "NULL NULL"
winMAIN gtk_window_new 0
gtk_window_set_default_size $winMAIN 400 200
gtk_window_set_title $winMAIN
gtk_window_set_position $winMAIN 1
tblMAIN gtk_table_new 10 10 1
gtk_container_add $winMAIN $tblMAIN
gtk_widget_show_all $winMAIN'


ipc() { eval $1=`gtk-server msg=$$,"$2 $3 $4 $5 $6 $7"`; }

echo "$CMDs" |while read CMD
do
   [ ! "$CMD" ]&& continue
   [ "`echo "$CMD" |grep '^#'`" ]&& continue

   if [ "`echo $CMD |grep '^gtk'`" ];then
echo -e '\n### CMD ###   '$(eval "echo \"$CMD\"")

     if [ "`echo $CMD |grep 'set_title'`" ];then
       ID=`gtk-server msg=$$,"$(eval "echo \"$CMD\"") 'Just a test title'"`
     else
      ID=`gtk-server msg=$$,"$(eval "echo \"$CMD\"") 1"`
     fi
echo '### RET = '"$ID"
   else
echo -e '\n### NEW ###   '"$CMD"
      ipc $(eval "echo \"$CMD\"")
echo '### $winMAIN = '$winMAIN'     ### $tblMAIN = '$tblMAIN
   fi
done

# Callback Wait
ipc $(eval "echo BlockAndWait 'gtk_server_callback WAIT'")

# Exit GTK-server
ipc $(eval "echo killserver 'gtk_server_exit'")



EDIT2:

Unfortunately, I think eval "echo \"$CMD\"" strips out the internal title quotes during evaluation. A complex way round that would be to temporarily substitute some rarely used character (such as @) for the double (or single) quote and then use bash (or sed) substitution to change it back to a double quote char after the eval is complete. I do stuff like that in my pcreole program to address similar unwanted bash unquoting effects.

_________________
Non enim propter gloriam, diuicias aut honores pugnamus set propter libertatem solummodo quam Nemo bonus nisi simul cum vita amittit.
Back to top
View user's profile Send private message Visit poster's website 
sunburnt


Joined: 08 Jun 2005
Posts: 4006
Location: Arizona, U.S.A.

PostPosted: Thu 13 Oct 2011, 02:31    Post subject:  

Thanks mcewanw; MAN is bash picky !!! . . . lol
Actually amigo was right too... Thanks amigo!
We all knew it was a quoting problem.

Another script gave me the clue. Quote the entire "gtk" command.
Code:
btnEXIT "gtk_button_new_with_label Exit"

I haven`t tried it in the loop code yet, but the errors were the same.
And I`m sure the parse loop was slower than just calling a function.
All it did was to remove the need for function calls on the gtk commands.
I may adapt it for the new Bash-4 coproc, but ipc seems to work well...

# I have my 3 file gtk-server setup working. It`s all bash and uses ipc.
It doesn`t need the .gtk4bash file.

User only writes gtk code file that`s control widgets only, and a bash event file.
The Main file ( gtkSrv_ipc.main ) should be good for almost any gui layout.

# I`ll have to ask Peter if the text header must stay... It`s bigger than the app.!

# NOTE: The gtk-server.cfg file is very useful for GTK+ command layout.
....... Hummm... Maybe a Gtk Help gui that uses the .cfg file? I`ll work at it...


### Reworked... Win. size and title, and Table rows/cols. are all settable.
gtkSrv.demo.zip
Description  # Run the file: gtkSrv_ipc.demo

### The files: gtkSrv_ipc.demo and gtkSrv_ipc.bash must be together.

### The file: gtkSrv_ipc.main must be in the path.

##### The file: gtkSrv_ipc.main is reusable and permanent.
.
zip

 Download 
Filename  gtkSrv.demo.zip 
Filesize  1.76 KB 
Downloaded  92 Time(s) 

Last edited by sunburnt on Fri 14 Oct 2011, 16:57; edited 3 times in total
Back to top
View user's profile Send private message 
sunburnt


Joined: 08 Jun 2005
Posts: 4006
Location: Arizona, U.S.A.

PostPosted: Thu 13 Oct 2011, 13:42    Post subject:  

Hey mcewanw; Your log code worked ( I missed it the first time ).
I didn`t notice that the demo. I worked with used the apps. path and file name.

Also I found using "sleep .1" ( 1/10 sec.) speeds up gui display ( of course...).
Code:
gtk-server -ipc=$$ -log="/tmp/$(basename $0).$$.log" &
sleep .1                  # start: gtk-server -ipc
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [9 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.0835s ][ Queries: 12 (0.0112s) ][ GZIP on ]