LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 03-17-2011, 04:11 AM   #1
nikefalcon
LQ Newbie
 
Registered: Jan 2011
Posts: 2

Rep: Reputation: 0
Exclamation Assignment to a variable variable.


This loop is part of a bash script which takes multiple arguments.
Code:
for ((i=1;i<=$number;++i)) ; do
	offset=$(($i+5))
	url$i=${!offset}
	echo $url1	
done
I get the following error-"./test2: line 6: url1=URLf1: command not found".

Note: "URLf1" is one of the arguments passed to the script.

Any ideas as to how to work around this??

Thanks

Last edited by nikefalcon; 03-17-2011 at 04:32 AM. Reason: typo
 
Old 03-17-2011, 06:21 AM   #2
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
You need to do variable expansion before the assignment -- a perfect job for the eval Bash built-in (see man bash-builtins for details). Try
Code:
for ((i=1;i<=$number;++i)) ; do
	offset=$(($i+5))
	eval url$i=${offset}
	eval echo "\\\$url\\\$i = \$url$i"
done
Note how the $ characters in the echo are encoded. eval is applied first; the resulting command (for i=1) is echo "\$url\$i = $url1". The escaping is sometimes pretty hairy, but it's easier if you work backwards: start from the expression you wish to execute, then add escaping, and finally prepend the eval to the command.

Hope this helps.
 
1 members found this post helpful.
Old 03-17-2011, 06:54 AM   #3
nikefalcon
LQ Newbie
 
Registered: Jan 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Thumbs up

Thank You, eval works perfectly.
So it now reads:
Code:
for ((i=1;i<=$number;++i)) ; do
    offset=$(($i+5))
    eval url$i=${!offset}
    ltemp=url$i
    URL=${!ltemp}
    echo CHECK$URL    
done
 
Old 03-17-2011, 07:56 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,006

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Personally I would try and use arrays instead of the needless call to eval. In general you will find that its use is often not required and generally cause more headaches than it is worth.
Obviously in the simple case as demonstrated you may not run into an issue.
 
1 members found this post helpful.
  


Reply



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
Question on variable naming and assignment... ${manintheyellowhat} Linux - Newbie 6 07-26-2010 07:41 PM
Variable assignment in Lisp. zaxonus Programming 2 03-18-2010 06:10 AM
[SOLVED] [BASH] variable assignment help RaptorX Programming 2 08-30-2009 10:28 AM
trivial: bash, local variable assignment agrestic Linux - Newbie 3 01-27-2009 11:30 AM
Java String variable re-assignment Cyhaxor Programming 9 11-24-2007 03:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:32 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration