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 Sat 22 Nov 2014, 06:45
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 Posts_count  
Author Message
Argolance


Joined: 06 Jan 2008
Posts: 1846
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.

Edited_times_total
Back to top
View user's profile Send_private_message Visit_website 
seaside

Joined: 11 Apr 2007
Posts: 887

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: 9256
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: 1846
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_website 
rcrsn51


Joined: 05 Sep 2006
Posts: 9256
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: 1846
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_website 
amigo

Joined: 02 Apr 2007
Posts: 2276

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: 1846
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_website 
amigo

Joined: 02 Apr 2007
Posts: 2276

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: 1846
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_website 
Display_posts:   Sort by:   
Page 1 of 1 Posts_count  
Post_new_topic   Reply_to_topic View_previous_topic :: View_next_topic
 Forum index » Off-Topic Area » Programming
Jump to:  

Rules_post_cannot
Rules_reply_cannot
Rules_edit_cannot
Rules_delete_cannot
Rules_vote_cannot
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0630s ][ Queries: 11 (0.0062s) ][ GZIP on ]