LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-07-2007, 01:47 PM   #1
ZuG
LQ Newbie
 
Registered: Jun 2003
Posts: 6

Rep: Reputation: 0
Bash Shell Scripting Dynamic Variable naming question


Hello,

I'm trying to name a variable dynamically such that the name is the contents of another variable plus some text.

Example:

variable $SITE=http://10.0.0.1
I want to create variable called "http://10.0.0.1DOWN" using the $SITE variable and then assign some value to it.

Something like:

"$SITE"DOWN=no

but of course that doesn't work. I get
bash: http://10.0.0.1DOWN=no: command not found.

How do I get it right?
 
Old 02-07-2007, 03:37 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Know two things.

First, a symbol in bash may not contain colons, slashes, or periods. Period. I recommend you change them all to underscores, or get rid of them altogether.

Second, bash can get confused if you do something like

$SYMBOL1=no

no matter what the current value of $SYMBOL is. You can get around that by using the "set" command (or the "export" command if you wish this symbol to be accessible by programs you run after that).

I recommend you play with something like this:

Code:
NEWSITE=$(echo $SITE | sed -e 's/[^A-Za-z0-9]/_/g' -e 's/$/DOWN/')
set ${NEWSITE}=no
Hope this helps.
 
Old 02-07-2007, 03:39 PM   #3
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,518

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
This is generally doable, but you first need to use a valid variable name, and "http://10.0.0.1DOWN" is not.

That one would work:
Code:
$ SITE=http_10_0_0_1
$ eval ${SITE}DOWN=no
$ eval echo ${SITE}DOWN
http_10_0_0_1DOWN
$ eval echo \$${SITE}DOWN
no
$ echo $http_10_0_0_1DOWN
no
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash shell scripting question. $Linuxnoob Fedora 13 04-01-2006 02:12 PM
shell (bash) scripting question(s) boxerboy Programming 5 11-08-2005 04:51 PM
Bash shell scripting question. pete1234 Programming 11 09-25-2005 03:59 AM
Bash shell scripting question. Itsu Linux - General 3 04-30-2005 04:52 AM
Bash shell scripting question mehesque Programming 2 07-15-2004 11:54 AM


All times are GMT -5. The time now is 02:19 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration