LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-29-2011, 11:07 PM   #1
Speedy2k
LQ Newbie
 
Registered: Nov 2008
Posts: 9

Rep: Reputation: 0
echoing a variable with a variable in its name?


I am building a script right now with variables with variable in their name created in a while loop. i would like to be able to output an invremental variable with echo like this: echo $RC$COUNT but i don't find the correct way to do it?

Code:
#!/bin/bash
#NOMBRE DE RELAIS
NR=8
#NOMBRE D'ENTREES NUMERIQUES
NIN=8
#NOMBRE D'ENTREES ANALOGIQUES
NIA=2
#NE PAS EDITER EN BAS DE CECI
#LES VALEURS DES RELAIS VONT ETRES STOCKER DANS LES VARIABLES: Rx
#LES VALEURS DES COMPTEURS DES RELAIS VONT ETRE STOCKER DANS LES VARIABLES: RCx
#LES VALEURS DES ENTREES NUMERIQUES VONT ETRES STOCKER DANS LES VARIABLES: INx
#LES VALEURS DES COMPTEURS DES ENTREES NUMERIQUES VONT ETRES STOCKER DANS LES VARIABLES: INCx
#LES VALEURS DES ENTREES ANALOGIQUES VONT ETERS STOCKER DANS LES VARIABLES: IAx
####################################################
#On va donner la valeur "" au 8 RELAIS et la valeur "0" au 8 compteurs
COUNT=1
while [ $COUNT -le $NR ]
do
	eval R${COUNT}=""
	eval RC${COUNT}="0"
	COUNT=$(( $COUNT + 1 ))
        echo $RC$COUNT
done
 
Old 12-30-2011, 01:59 AM   #2
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
I'm not sure what you want to achieve, but here are my two best guesses.

arrays

Code:
$ fruit[0]="apple"; fruit[1]="orange"
$ i=0; while [ $i -le 1 ]; do echo ${fruit[$i]}; i=$(( $i + 1 )); done
apple
orange
$
indirect expansion

Code:
$ animal="horse"; color="brown"
$ for i in animal color; do echo ${!i}; done
horse
brown
$
HTH

Last edited by Telengard; 12-30-2011 at 02:02 AM.
 
Old 12-30-2011, 08:45 AM   #3
Speedy2k
LQ Newbie
 
Registered: Nov 2008
Posts: 9

Original Poster
Rep: Reputation: 0
I don't really understand how to achieve that with an array or even if i am explaining myself correctly, but what i achieve to do is echoing a incremental variable into a loop.
So here it will loop until count equal 8. So in the first loop i want to echo $RC1 in this case will be zero on all 8, but on the next loop it will echo the value of $RC2 etc. in this case will always be 0.
 
Old 12-30-2011, 01:13 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Everything you describe is just screaming for arrays.

Arrays are one of the most useful, and least used, features of the modern shell. So take some time to learn how to use them. Seriously. They're very simple really, just a linked list of variables, all with the same name, but with individual index numbers to differentiate the individual entries.

Basically, instead of RC1, RC2, etc. you use RC[1],RC[2], etc. (actually the count is zero-based, so you'd generally be starting with [0]).

http://mywiki.wooledge.org/BashGuide/Arrays

Your loop above could be written like this, for example (also using a c-style for loop for counting instead):
Code:
for (( i=1;i<=8;i++ )); do			#Loops through the numbers 1-8
	R[i]=""					#sets the R array element with the value of $i to empty
	RC[i]="0"				#sets the RC array element with the value of $i to 0
	echo "The value of RC[$i] is ${RC[i]}"	#echo back the current array element
done

echo "${RC[@]}"		#to list all entries at once.
I highly suggest you read the whole BashGuide, from the link I gave. It gives good, clear explanations of all the fundamental concepts you should understand for bash scripting.

As Telengard also pointed out, there are other techniques for indirect variable use, but they are generally unnecessary when you have arrays available.

http://mywiki.wooledge.org/BashFAQ/006
 
1 members found this post helpful.
Old 12-31-2011, 12:47 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,120

Rep: Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345Reputation: 1345
I agree that what you are doing cries out for arrays, but to directly answer your question, you need to use the 'eval' built-in command to re-parse the line:
Code:
XX21=something
BASE=XX
COUNT=21
eval echo \$$BASE$COUNT
Use of 'eval' can get very messy very quickly because any character special to the shell is getting parsed twice, so you sometimes end up needing a lot of backslash characters (and perhaps some minor cursing and use of "set -x" to see what the heck is happening) to get the final effect right.
 
Old 12-31-2011, 02:01 PM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
eval also has potentially serious security implications, and for the same reason--it executes the line twice. Unless you know exactly what the input values will be, and exactly what happens when the line is eval'd, you run the danger of bad things happening (either accidentally or by design).

http://mywiki.wooledge.org/BashFAQ/048

My position is, basically, do not use eval. Of course I can't add ever because there are times when it genuinely is the only solution. But 99% of the time as soon as you find yourself typing "e..v..a...", it's time to stop what you're doing and reevaluate the whole process instead. There are almost certainly other, better ways to do what you want.
 
  


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
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 01:44 AM
[SOLVED] How can you create aliases with echoing text into the variable? BMan8577 Linux - Newbie 4 04-29-2011 02:00 PM
AWK a variable Ouptut to a new variable and using the new variable with the old one alertroshannow Linux - Newbie 4 02-16-2009 01:08 AM
Sed search for variable and delete entire line, but variable contains forward /'s Passions Programming 2 11-10-2008 04:44 PM


All times are GMT -5. The time now is 07:57 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration