1757749 Members
1927 Online
108863 Solutions
New Discussion юеВ

loop counter script

 
SOLVED
Go to solution
Ratzie
Super Advisor

loop counter script

How do you go about doing a loop that counts from 00001 to 99999

This errors out...

echo $COUNT
while [ "$COUNT" -lt "1" ]
do
COUNT=`expr "$COUNT" + 00001`
# echo $COUNT"," >> $DATAFILE
echo $COUNT","
done
5 REPLIES 5
Jean-Luc Oudart
Honored Contributor
Solution

Re: loop counter script

initialise COUNT :

typeset -i COUNT
COUNT=0

Rgds,
JL
fiat lux
Victor BERRIDGE
Honored Contributor

Re: loop counter script

You havent initialized COUNT for a start...


Good luck

Victor
Ratzie
Super Advisor

Re: loop counter script

Sorry but newbie, intialize?
When I added to my script I get an error...

#!/bin/sh

#Variables
DATAFILE=/tmp/count.file
initialize COUNT:
typeset -i COUNT
COUNT=0


#start script
#does counter equal 99999
echo $COUNT
while [ "$COUNT" -lt "1" ]
do
COUNT=`expr "$COUNT" + 00001`
# echo $COUNT"," >> $DATAFILE
echo $COUNT","
done


./count_to.sh[20]: initialize: not found.
A. Clay Stephenson
Acclaimed Contributor

Re: loop counter script

You don't need expr and you should avoid [ rather then [[. In the POSIX or Korn shell if [[ ]] is an internal test whereas if [] calls the external test command. Also (( .. )) is an implicit let for numeric operations.

typeset -Z5 COUNT=1
typeset -Z5 STOP=99999
while [[ ${COUNT} -le ${STOP} ]]
do
echo "${COUNT},"
(( COUNT +=1 ))
done
If it ain't broke, I can fix that.
Michael Schulte zur Sur
Honored Contributor

Re: loop counter script

Hi,

initialize means setting a variable to a defined value, in your case COUNT=1.

greetings,

Michael