Operating System - HP-UX
1751975 Members
4562 Online
108784 Solutions
New Discussion юеВ

Increment a variable name

 
SOLVED
Go to solution
UXisCool
Advisor

Increment a variable name

How could I take a variable name's that contains a number at the end and increment the number part? This is possible in basic, I would think Unix scripts would allow it too but it always fails.
simple example
count = 0
count=`expr $count + 1`
# 1st time thru count would be equal to 1

tm$count="69"
echo $tm1

$tm1 fails and does not exist
5 REPLIES 5
James R. Ferguson
Acclaimed Contributor

Re: Increment a variable name

Hi:

More effieiently, since using 'expr' spawns another process, use the shell's arithmetic abilities:

# X=1
# echo ${X{
1
X X=$((X+1))
# echo ${X}
2

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Increment a variable name

Hi (again) :

Sorry, there's a typo. My post should be:

# X=1
# echo ${X}
1
X X=$((X+1))
# echo ${X}
2

Regards!

...JRF...
OldSchool
Honored Contributor
Solution

Re: Increment a variable name

jrf....

I think you missed what he's looking for,

he appears to want to generate named variables in the form of
x1, x2 , x3...x by incrementing some varible.

something along the lines of:

typeset -i cnt
cnt=0

eval tm$cnt=a
cnt=$cnt+1
eval tm$cnt=b
echo $tm0 $tm1
UXisCool
Advisor

Re: Increment a variable name

Old School hit the nail on the head of what I was looking for. I tried to submit you 10 points ... somehow it changed to 8 when I had scrolled down but THANKS!!!!!
OldSchool
Honored Contributor

Re: Increment a variable name

ksh and other shells also support arrays (i.e. subscripted variables) that might be easier to manage....