Author |
Message |
Argolance

Joined: 06 Jan 2008 Posts: 3255 Location: PORT-BRILLET (Mayenne - France)
|
Posted: Mon 18 Mar 2013, 10:28 Post subject:
How to put a variable inside a variable? [SOLVED] |
|
Hello,
In the following strings, I would like the variable "APP" to work:
Code: | 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...)
Thanks.
Cordialement.
Last edited by Argolance on Wed 20 Mar 2013, 09:18; edited 2 times in total
|
Back to top
|
|
 |
seaside
Joined: 11 Apr 2007 Posts: 917
|
Posted: Mon 18 Mar 2013, 12:01 Post subject:
|
|
Argolance,
I think the variable APP is not being resolved before the call - try placing double quotes around like this....
Code: | "$(gtkdialog4 --program=${APP}_install --center)" |
Cheers,
s
|
Back to top
|
|
 |
rcrsn51

Joined: 05 Sep 2006 Posts: 11889 Location: Stratford, Ontario
|
Posted: Mon 18 Mar 2013, 13:31 Post subject:
|
|
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.
|
Back to top
|
|
 |
Argolance

Joined: 06 Jan 2008 Posts: 3255 Location: PORT-BRILLET (Mayenne - France)
|
Posted: Mon 18 Mar 2013, 13:34 Post subject:
|
|
Hello,
Thanks for replying.
This works fine if the variable is a single word like "seamonkey" for example , but not if the variable is "google-chrome"(with a hyphen)...  Quote: | /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: | 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!
Strange!
Cordialement.
|
Back to top
|
|
 |
rcrsn51

Joined: 05 Sep 2006 Posts: 11889 Location: Stratford, Ontario
|
Posted: Mon 18 Mar 2013, 13:44 Post subject:
|
|
That's because a hyphen is not a valid character in a variable name.
|
Back to top
|
|
 |
Argolance

Joined: 06 Jan 2008 Posts: 3255 Location: PORT-BRILLET (Mayenne - France)
|
Posted: Mon 18 Mar 2013, 13:56 Post subject:
|
|
I never had the opportunity to notice this... till today! That means I never named any variable with a hyphen!
Thank you.
Cordialement.
|
Back to top
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 2641
|
Posted: Mon 18 Mar 2013, 14:08 Post subject:
|
|
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
|
Back to top
|
|
 |
Argolance

Joined: 06 Jan 2008 Posts: 3255 Location: PORT-BRILLET (Mayenne - France)
|
Posted: Tue 19 Mar 2013, 04:33 Post subject:
|
|
Bonjour,
Quote: | 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".
"!" ??
What does it mean?
Cordialement.
|
Back to top
|
|
 |
amigo
Joined: 02 Apr 2007 Posts: 2641
|
Posted: Tue 19 Mar 2013, 14:35 Post subject:
|
|
What does it mean? It does exactly what the original question was about:
Code: | #!/bin/bash
A=10
B=3
C=4
for VAR in A B C ; do
echo ${!VAR}
done
|
|
Back to top
|
|
 |
Argolance

Joined: 06 Jan 2008 Posts: 3255 Location: PORT-BRILLET (Mayenne - France)
|
Posted: Wed 20 Mar 2013, 04:29 Post subject:
|
|
Thank you!
|
Back to top
|
|
 |
|