counter in a variable - can it be done?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting counter in a variable - can it be done?
# 1  
Old 08-09-2007
counter in a variable - can it be done?

I have the following

Code:
for(( i=1; 1<=2; i++))
do
e1=123 n1=123
e2=456 n2=456
coord= $e1,$n1
echo "coordinate=$coord"
done
exit

this echos
coordinate=123,123

I need it to loop so:
loop1
coord=$e1,$n1
loop2
coord=$e2.$n2

then it echos:

coordinate=123,123
coordinate=456,456

I have been trying to write it like this:
coord=$e${i},$n${i}

but it doesn't work any ideas

Thanks

Gary
# 2  
Old 08-09-2007
In Korn shell -- a personal favorite -- there are two ways of doing it both of which you have considered (I've changed the numbers a bit to make it clearer:

1 ) As an array

Code:
#!/bin/ksh

set -A e 123 456
set -A n 234 567

I=0

while [ $I -lt ${#e[*]} ]
do
    echo "coordinate=${e[$I]}, ${n[$I]}"
    (( I += 1 ))
done
exit


coordinate=123, 234
coordinate=456, 567


or using individual variables:

Code:
#/bin/ksh

e1=123; n1=234
e2=456; n2=567

for I in 1 2
do
    eval echo "coordinate=\$e$I, \$n$I"
done
exit

coordinate=123, 234
coordinate=456, 567
# 3  
Old 08-09-2007
the latter nearly worked but just displayed
coordinate=$e1,$n1
coordinate=$e2,$n2

I need it to replace these with the actual values
# 4  
Old 08-09-2007
It worked for me once I fixed the first line...
Code:
$ cat x
#!/usr/bin/ksh

e1=123; n1=234
e2=456; n2=567

for I in 1 2
do
    eval echo "coordinate=\$e$I, \$n$I"
done
exit
$ ./x
coordinate=123, 234
coordinate=456, 567
$

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Pegging counter

Hi Experts, I am in need for some help. My competence level on unix is not at all helping me to resolve this. Please help. My Input for a system command is as under: Counters are getting pegged each hour. I need to have a difference printed rather than pegged counter values. Counter... (2 Replies)
Discussion started by: vanand420
2 Replies

2. Shell Programming and Scripting

How to take input from different files for using the counter variable?

Hi All, I have script which call test utility and it takes token input which is kept in a separate file. now instead of taking tokens from single file,will keep 5 input files and script should read tokens from each files. EX : There will 5 token.txt file which will have say around 1000... (1 Reply)
Discussion started by: Optimus81
1 Replies

3. Shell Programming and Scripting

Help require with counter

I have this file 1801,A1,2012-12-16 15:59:59.995,id2_3,ab,phoneC2-00,VOE,,,,,,,,,,,,,,, 1802,A1,2012-12-16 15:59:59.995,id2_3,ab=,phoneX1-01,BL,,,,,,,,,,,,,,, 1803,A1,2012-12-16 15:59:59.995,id2_3,ab,phone300,BL,,,,,,,,,,,,,,, 1804,A1,2012-12-16... (4 Replies)
Discussion started by: nikhil jain
4 Replies

4. Shell Programming and Scripting

Counter

if ;then echo "mrnet greater 5000" gzip /var/log/mrnet.log mv /var/log/mrnet.log.gz /var/log/mrnet.log.1.gz if ];then i=1 let i++ mv /var/log/mrnet.log.1.gz /var/log/vieux-logs/mrnet.log.$i.gz else echo "theres no... (1 Reply)
Discussion started by: Froob
1 Replies

5. Shell Programming and Scripting

problem with counter

i having a file xxxxxxxxxxxxxxx1234 ...........value can be change xxxxxxxxxxxxxxx1235 xxxxxxxxxxxxxxxx1236 . . . . xxxxxxxxxxxxxxxxx1300 ...........value can be change i want to cut last four characters of first line and last line and find the missing pattern. output should... (4 Replies)
Discussion started by: sagar_1986
4 Replies

6. Shell Programming and Scripting

DelimiterCount: how to use a counter

Hi, I am new to shell script. I want to count to Delimiter count for my source file. For that I have written script. When I tried to execute the script I could not able to view the results. It throws errors. I don't know what the problem is. My aim is I want to store the delimiter count in one... (4 Replies)
Discussion started by: suresh01_apk
4 Replies

7. Shell Programming and Scripting

adding counter to a variable while moving in a loop

The scenario is like this : I need to read records from a file one by one and increment counter1, if a certain field matches with a number say "40"..the script should increment the counter2 and also extract a corresponding field from the same line and adding them one by one and redirecting the the... (5 Replies)
Discussion started by: mady135
5 Replies

8. Shell Programming and Scripting

counter

Hi, I need some help. Shell script counter. i need to add condition to check if counter is more than 10 and longer than 3 hours? it runs every 5 mins. it only check count and send email right now. it runs in cron as below gregcount.ksh gregdb 10 > /tmp/gregcount.out 2> /tmp/gregcount.err ... (1 Reply)
Discussion started by: pega
1 Replies

9. Shell Programming and Scripting

Counter Script..help please

I have generated a script that will email a list of people if a certain PID is not running, using "mailx". I have the script running every 5 minutes as a cron job. I want the script to stop sending an email, if the email has been sent 5 times (meaning PID is dead). I want this so that my... (3 Replies)
Discussion started by: Sunguy222
3 Replies

10. Shell Programming and Scripting

Loop counter variable display

Hello everyone, how can I send output to the screen from a running script or tcl, in such a way that if a loop is executing I will see the rolling counter on my screen as the records are processed in the loop. I do not want the screen to scroll, though. In other words can a var's value be painted... (2 Replies)
Discussion started by: lifespan
2 Replies
Login or Register to Ask a Question