Unix Shell - [KSH] using integers for testing while loops

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > April 2004 > [KSH] using integers for testing while loops





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author [KSH] using integers for testing while loops
Andrew Falanga

2004-03-26, 6:38 pm

Ok,

This one I'm not sure how to fix. Here's the basic concept:

typeset -i i=0
typeset -i max=10

while (( i < max ))
do
# doing stuff
done

Now, I'm using KSH '88 in HP-UX 11. I have tried variations such as:

while $(( i < max ))
while $(( $i < $max ))
while (( i -lt max ))
while (( $i -lt $max ))
while [ $i -lt $max ]
.......ad nauseum

What am I doing wrong? I've tried a few things from past posting to
this group, but here I am. Sorry for the ignorance.

Thanks,

---------------------------------------------
Andrew R. Falanga (a non-HP employee)
Hewlett-Packard Company
11311 Chinden Blvd.
Boise, Idaho
---------------------------------------------
Please note: The e-mail address is purposely
mangled. I do not wish my account at HP to
become a spam haven.
joe@invalid.address

2004-03-26, 6:38 pm

Andrew Falanga <falandrew@hp.com> writes:

> Ok,
>
> This one I'm not sure how to fix. Here's the basic concept:
>
> typeset -i i=0
> typeset -i max=10
>
> while (( i < max ))
> do
> # doing stuff
> done


When the loop starts i is less than max, but you don't increment it
anywhere.

#!/bin/ksh
typeset -i i=0
typeset -i max=10

while (( i < max ))
do
echo i = $i
((i = i+1))
done

This is on HP-UX 10.20 with its ksh88.

Joe
--
The Dutchman still wears wooden shoes, his cap and coat are patched
with the love that Magaret sewed there...
- Steve Goodman
bsh@iname.com

2004-04-21, 9:34 pm

joe@invalid.address wrote in message news:<m37jx7xlud.fsf@invalid.address>...
> Andrew Falanga <falandrew@hp.com> writes:
> while (( i < max ))
> do
> echo i = $i
> ((i = i+1))
> done


A slightly more efficient and elegant technique is:

typeset -i i=-1 max=10 # note: i is pre-decremented to -1
while ((i+=1)<max))
do print i = $i
done

=Brian
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com