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 Thu 28 Aug 2014, 07:38
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
How to put a variable inside a variable? [SOLVED]
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [10 Posts]  
Author Message
Argolance


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

PostPosted: 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...) Embarassed
Thanks.

Cordialement.

Last edited by Argolance on Wed 20 Mar 2013, 09:18; edited 2 times in total
Back to top
View user's profile Send private message Visit poster's website 
seaside

Joined: 11 Apr 2007
Posts: 886

PostPosted: 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
View user's profile Send private message 
rcrsn51


Joined: 05 Sep 2006
Posts: 9058
Location: Stratford, Ontario

PostPosted: 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
View user's profile Send private message 
Argolance


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

PostPosted: 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 Very Happy , but not if the variable is "google-chrome"(with a hyphen)... Embarassed
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!
Shocked
Strange!

Cordialement.
Back to top
View user's profile Send private message Visit poster's website 
rcrsn51


Joined: 05 Sep 2006
Posts: 9058
Location: Stratford, Ontario

PostPosted: 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
View user's profile Send private message 
Argolance


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

PostPosted: 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
View user's profile Send private message Visit poster's website 
amigo

Joined: 02 Apr 2007
Posts: 2236

PostPosted: 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
View user's profile Send private message 
Argolance


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

PostPosted: 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".
Quote:
${!APP}
"!" ??
What does it mean? Embarassed

Cordialement.
Back to top
View user's profile Send private message Visit poster's website 
amigo

Joined: 02 Apr 2007
Posts: 2236

PostPosted: 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
View user's profile Send private message 
Argolance


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

PostPosted: Wed 20 Mar 2013, 04:29    Post subject:  

Thank you!
Back to top
View user's profile Send private message Visit poster's website 
Display posts from previous:   Sort by:   
Page 1 of 1 [10 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.0620s ][ Queries: 12 (0.0046s) ][ GZIP on ]