Simple LOOP script help

I am trying to create a simple ksh loop script. What I've pasted below is not working. Any help is appreciated.

typeset -i count
typeset -i tcount
count=0
tcount=0
while $tcount >= 11
do
print "\$count is $count"
pwd
ls -l
sleep 30
$(( $tcount = $count + 1 ))
$count=$tcount
done

Well, you set the variable to 0 and then loop while it is >= 11.
It will, quite correctly, loop exactly 0 times.
Try chaning the >= to <= and see what happens.

I tried the change but it didn't help.. here is the result. Any other idea's... Thanks for your help with this.

$ cat maldo_sleep
typeset -i count
typeset -i tcount
count=0
tcount=0
while $tcount <= 11
do
print "\$count is $count"
pwd
ls -l
sleep 30
$(($tcount=$count+1))
$count=$tcount
done
$ ./maldo_sleep
./maldo_sleep[7]: 0: not found
$

Hi!

There are actually two errors in your script:

  1. testing a variable doesn't work that way. You have to use test - external or built-in, I don't remember how this is in ksh - hence, I'll show the "old-fashioned way" :slight_smile: see your ksh manual page, look for keyword test.
  2. you are using < in your test. for shell, less-than means input redirection from a file

So, this should work:

    while test $tcount -le 11
    # ... your code here
    done

I also don't quite understand the way incrementation of tcount is done. Simple

tcount=$(($tcount+1)) would do.

Regards,

pen

Why not something simple, like
for foo in 0 1 2 3 4 5 6 7 8 9 10 ; do
echo "Count is ${foo} "
pwd
ls -l
sleep 30
done

if you are going to iterate over a known and manageable range like this, for MAY be more appropriate than while.

A sample ksh loop :

$ cat loop.ksh
typeset -i count=1
typeset -i limit=10

while (( count <= limit ))
do
   echo "count=$count"
   (( count += 1 ))
done

$ loop.ksh
count=1
count=2
count=3
count=4
count=5
count=6
count=7
count=8
count=9
count=10
gssjgu:/g/g00k00/gssjgu/TMP> 

With ksh93 you can also do :

$ cat for_loop.ksh 
typeset -i limit=10

for (( count=1; count<=limit; ++count ))
do
   echo "count=$count"
done

$ for_loop.ksh
count=1
count=2
count=3
count=4
count=5
count=6
count=7
count=8
count=9
count=10
$ 

Jean-Pierre.