Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic variable name 4

Status
Not open for further replies.

TSch

Technical User
Jul 12, 2001
557
0
0
DE
Hi folks,

again I'm trying to write a shell script and again I'm facing a problem ...

What I'd like to do is the following:

Code:
Counter=0
while [ $Counter -le 3 ];
do
InputX=$(cat /tmp/lst$Counter | awk '{print $1}')
Counter=`expr $Counter + 1`
done

Here's the tricky part:
I don't want the variable to be named InputX !
It's supposed to change according to the $Counter value so that it looks like this:

$Counter=0 -> Variable is supposed to be named Input1
$Counter=1 -> Variable is supposed to be named Input2
... and so on

What I tried was the following:

Code:
Input$Counter=$(cat /tmp/lst$Counter | awk '{print $1}')

But that's not working ...
What would be the correct approach here ?

Regards
Thomas
 
Hi, think eval if you want some pointer behaviour that works in two times

Code:
Counter=0
while [ $Counter -le 3 ];
do
        eval X=Input$Counter
        echo $X
        eval $X=$(cat /tmp/lst$Counter | awk '{print $1}')
        eval Y=\$Input$Counter
        echo $Y
        Counter=`expr $Counter + 1`
done
 

Same code with reuse X variable

Code:
Counter=0
while [ $Counter -le 3 ];
do
        eval X=Input$Counter
        echo $X
        eval $X=$(cat /tmp/lst$Counter | awk '{print $1}')
        eval Y=\$$X
        echo $Y
        Counter=`expr $Counter + 1`
done
 
Hi

Why not use an array ?
Code:
[navy]Counter[/navy][teal]=[/teal][purple]0[/purple]
[b]while[/b] [teal][[/teal] [navy]$Counter[/navy] -le [purple]3[/purple] [teal]];[/teal] [b]do[/b]
  Input[highlight][teal][[/teal][navy]Counter[/navy][teal]][/teal][/highlight][teal]=[/teal][navy]$([/navy]cat /tmp/lst[navy]$Counter[/navy] [teal]|[/teal] awk [green][i]'{print $1}'[/i][/green][teal])[/teal]
  [navy]Counter[/navy][teal]=[/teal]`expr [navy]$Counter[/navy] [teal]+[/teal] [purple]1[/purple]`
[b]done[/b]
No idea what shall are you using, but I would prefer C-like syntax :
Code:
[navy]Counter[/navy][teal]=[/teal][purple]0[/purple]
[b]while[/b] [teal](([/teal] Counter[teal]<=[/teal][purple]3[/purple] [teal]));[/teal] [b]do[/b]
  Input[teal][[/teal]Counter[teal]]=[/teal][navy]$([/navy]cat /tmp/lst[navy]$Counter[/navy] [teal]|[/teal] awk [green][i]'{print $1}'[/i][/green][teal])[/teal]
  [teal](([/teal] Counter[teal]++[/teal] [teal]))[/teal]
[b]done[/b]

Feherke.
 
$Counter=0 -> Variable is supposed to be named Input1
What about this ?
Code:
Counter=0
while [ $Counter -le 3 ]; do
  X=$(awk '{print $1}' /tmp/lst$Counter)
  ((Counter+=1))
  eval Input$Counter="$X"
done

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Folks, you're great !
Thanks a lot for all the input ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top