How to put a variable inside a variable? [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:

How to put a variable inside a variable? [SOLVED]

#1 Post by Argolance »

Hello,
In the following strings, I would like the variable "APP" to work:

Code: Select all

APP=app #example: seamonkey or any other!
I=$IFS; IFS=""
for STATEMENTS in  $(gtkdialog4 --program=${APP}_install --center); do
   eval $STATEMENTS
done
IFS=$I
How could this be done? Tried many things (eval and so on...) :oops:
Thanks.

Cordialement.
Last edited by Argolance on Wed 20 Mar 2013, 13:18, edited 2 times in total.

seaside
Posts: 934
Joined: Thu 12 Apr 2007, 00:19

#2 Post by seaside »

Argolance,

I think the variable APP is not being resolved before the call - try placing double quotes around like this....

Code: Select all

"$(gtkdialog4 --program=${APP}_install --center)"
Cheers,
s

User avatar
rcrsn51
Posts: 13096
Joined: Tue 05 Sep 2006, 13:50
Location: Stratford, Ontario

#3 Post by rcrsn51 »

Argolance: I tried your example and it worked for me.

Just remember that if APP has the value "seamonkey", then "seamonkey_install" must be the name of a variable that contains a GTKdialog program, and has been exported.

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

#4 Post by Argolance »

Hello,
Thanks for replying.
This works fine if the variable is a single word like "seamonkey" for example :D , but not if the variable is "google-chrome"(with a hyphen)... :oops:
/usr/local/bin/2P_browser_install: line 29: export: `google-chrome_install=
<window title="google-chrome-23.0.1271.97 Installation" decorated="true">
<vbox border-width="20">
<pixmap><input file>/usr/share/pixmaps/2P_apps_install/google-chrome_logo.png</input></pixmap>
<hseparator></hseparator>
<hbox homogeneous="true">
<button><label>"Installer le paquet"</label><input file>/usr/local/lib/X11/pixmaps/mntd.png</input><action>EXIT:package</action></button>
<button><label>"Télécharger le fichier SFS"</label><input file>/usr/local/lib/X11/pixmaps/mntd_boot.png</input><action>EXIT:sfs</action></button>
<button><label>"Annuler"</label><input file>/usr/local/lib/X11/pixmaps/_.png</input><action>EXIT:cancel</action></button>
</hbox>

</vbox>
</window>
': not a valid identifier

** (gtkdialog4:2435): ERROR **: Gtkdialog: Could not find the dialog description in the environment variable 'google-chrome_install'.
Script completed hit RETURN to close window.
I didn't notice this before but, if a string like "google-chrome"with a hypen is specified...

Code: Select all

export google-chrome="
...
"
I=$IFS; IFS=""
for STATEMENTS in  "$(gtkdialog4 --program=google-chrome --center)"; do
   eval $STATEMENTS
done
IFS=$I
... directly inside a script, the same error message is displayed!
:shock:
Strange!

Cordialement.

User avatar
rcrsn51
Posts: 13096
Joined: Tue 05 Sep 2006, 13:50
Location: Stratford, Ontario

#5 Post by rcrsn51 »

That's because a hyphen is not a valid character in a variable name.

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

#6 Post by Argolance »

I never had the opportunity to notice this... till today! That means I never named any variable with a hyphen!
Thank you.

Cordialement.

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

#7 Post by amigo »

This is what you mean -but it is bash-specific: ${!APP}

For other shells you have to use a complex eval+echo+eval command to duplicate that functionality

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

#8 Post by Argolance »

Bonjour,
That means I never named any variable with a hyphen?
I was thinking: "When the value of a variable becomes part of a variable itself".
${!APP}
"!" ??
What does it mean? :oops:

Cordialement.

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

#9 Post by amigo »

What does it mean? It does exactly what the original question was about:

Code: Select all

#!/bin/bash

A=10
B=3
C=4

for VAR in A B C ; do
	echo ${!VAR}
done

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

#10 Post by Argolance »

Thank you!

Post Reply