Puzzled by variable evaluation problem. [ 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
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

Puzzled by variable evaluation problem. [ Solved ]

#1 Post by sunburnt »

This code echoes the variable name and not it`s contents:

Code: Select all

#!/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, 07:53, edited 1 time in total.

amigo
Posts: 2629
Joined: Mon 02 Apr 2007, 06:52

#2 Post by amigo »

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.

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#3 Post by sunburnt »

Thanks amigo; I`ve tried that, it displays this:

Code: Select all

# 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???

mcewanw
Posts: 3169
Joined: Thu 16 Aug 2007, 10:48
Contact:

#4 Post by mcewanw »

@sunburnt:

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

Code: Select all

logfile="/tmp/$(basename "$0")"
gtk-server -ipc=$$ -log="$logfile" &
Last edited by mcewanw on Thu 13 Oct 2011, 03:05, edited 1 time in total.
github mcewanw

mcewanw
Posts: 3169
Joined: Thu 16 Aug 2007, 10:48
Contact:

#5 Post by mcewanw »

@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: Select all

#!/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
Last edited by mcewanw on Thu 13 Oct 2011, 03:04, edited 1 time in total.
github mcewanw

mcewanw
Posts: 3169
Joined: Thu 16 Aug 2007, 10:48
Contact:

#6 Post by mcewanw »

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: Select all

#!/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
github mcewanw

mcewanw
Posts: 3169
Joined: Thu 16 Aug 2007, 10:48
Contact:

#7 Post by mcewanw »

@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: Select all

#!/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.
github mcewanw

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#8 Post by sunburnt »

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: Select all

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.
Attachments
gtkSrv.demo.zip
# 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.
.
(1.76 KiB) Downloaded 253 times
Last edited by sunburnt on Fri 14 Oct 2011, 20:57, edited 3 times in total.

User avatar
sunburnt
Posts: 5090
Joined: Wed 08 Jun 2005, 23:11
Location: Arizona, U.S.A.

#9 Post by sunburnt »

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: Select all

gtk-server -ipc=$$ -log="/tmp/$(basename $0).$$.log" &
sleep .1						# start: gtk-server -ipc

Post Reply