Visit The New, Modern Unix Linux Community

printing variable with variable suffix through loop

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting printing variable with variable suffix through loop
# 1  
printing variable with variable suffix through loop

I have a group of variables myLINEcnt1 - myLINEcnt10. I'm trying to printout the values using a for loop. I am at the head banging stage since i'm sure it has to be a basic syntax issue that i can't figure out.

For myIPgrp in 1 2 3 4 5 6 7 8 9 10; do
here i want to output the value of myLINEcnt[1-10]

but every attempt i've tried either errors or give me just the number portion (1-10)

echo ${myLINEcnt}${myIPgrp}

Crap, i'm an idiot. SORRY I'm using bash on a Centos 5.7 system.

Thanks in advance.

Last edited by oly_r; 06-26-2012 at 10:50 AM.. Reason: left out basic info.
# 2  
You need to use eval to access dynamically generated variable names. And, as a further hint, with eval you'll need to quote the first $. Since eval can execute arbitrary code, use it with caution. This particular use should be fine. Read your shell man page for the details.

Your shell may provide a better alternative. Perhaps arrays. Or references. If you want help with the bigger picture, tell us which shell and os you're using and what you're trying to accomplish.


Last edited by alister; 06-26-2012 at 09:44 AM..
# 3  
Crap, i'm an idiot. SORRY I'm using bash on a Centos 5.7 system.
# 4  
for i in myIPgrp{1..10}; do
         echo ${!i}

This User Gave Thanks to complex.invoke For This Post:
# 5  
Originally Posted by huaihaizi3
for i in myIPgrp{1..10}; do
         echo ${!i}

Well crud, i figured it was an easy answer but DUH!. Never done it that way but i will be, thanks.

what is the ! doing???

Oh i actually use

for i in myLINEcnt{1..10} do

I will still be looking at the eval method, getting that to work will help me in the future.

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #792
Difficulty: Easy
In mathematics and digital electronics, a binary number is a number expressed in the base-2 numeral system or binary numeral system.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable substitution in array printing

Hi folks, A really dumb question as I've wasted far too long trying to get this to work.... (on RH bash) I have an array: m0='<hello>' m0='<there>' m0='<fred>' v0='<goodbye>' v0='<again>' v0='<john>' in my code I calculate the value of the variable to output and if I echo it, I... (2 Replies)
Discussion started by: say170
2 Replies

2. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

3. Shell Programming and Scripting

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

Hello All, Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....? I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping through a string containing some of these "Illegal Characters". Now... (5 Replies)
Discussion started by: mrm5102
5 Replies

4. UNIX for Dummies Questions & Answers

Loop and variable not exactly variable: what's wrong

Hello guys, This truly is a newbie question. I'm trying to make a loop to execute simultaneous commands indefinitely while using variable. Here is how my mess looks like (this is just an example): #!/bin/bash IP=`shuf -n 1 IP.txt` # I figured this would be easier to select random lines... (4 Replies)
Discussion started by: bobylapointe
4 Replies

5. Shell Programming and Scripting

[SHELL: /bin/sh] For loop using variable variable names

Simple enough problem I think, I just can't seem to get it right. The below doesn't work as intended, it's just a function defined in a much larger script: CheckValues() { for field in \ Group_ID \ Group_Title \ Rule_ID \ Rule_Severity \ ... (2 Replies)
Discussion started by: Vryali
2 Replies

6. Homework & Coursework Questions

problem with printing out variable in awk

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: couldn't print out stored variable in awk 2. Relevant commands, code, scripts, algorithms: i have in a... (5 Replies)
Discussion started by: ymc1g11
5 Replies

7. Shell Programming and Scripting

Printing a variable column using awk

Hi everyone, Ok here's the scenario. I have a control file like this. component1,file1,file2,file3,file4,file5 component2,file1,file2,file3,file4,file5I want to do a while loop here to read all files for each component. file_count=2 while ] do file_name=`cat list.txt | grep... (2 Replies)
Discussion started by: The Gamemaster
2 Replies

8. Shell Programming and Scripting

Redirecting stdout to variable while printing it

Hi everybody, I am trying to do the thing you see in the title, and I can't simply do a=$(svn up) echo $a because the program (svn) gives output on lots of lines and in the variable the output is stored on only one line (resulting in a horribly formatted text). Any tips? Thanks,... (2 Replies)
Discussion started by: ocirne94
2 Replies

9. Shell Programming and Scripting

Replace variable with its value while printing

Hi I have a file in which there is list of files. eg: $path1/file1 $path1/file2 $path2/file3 I am trying to read this file in other script.However the value of variable i.e. $path1 and $path2 is not replaced by its value. How to do it ? I am trying: while read line do echo... (2 Replies)
Discussion started by: dashing201
2 Replies

10. UNIX for Dummies Questions & Answers

sed not printing variable

I have the following string stored in a variable from a file sbCvgXfsuupllsucpp11-aWa I want to use sed to search for the string in a second file: FileSys.dat sed -n "${obid}" FileSys.dat I'm getting "sed: command garbled: sbCvgXfsuupllsucpp11-aWa" The syntax seems fine...anyone... (2 Replies)
Discussion started by: orahi001
2 Replies

Featured Tech Videos