1827197 Members
2468 Online
109716 Solutions
New Discussion

for i in ????

 
SOLVED
Go to solution
David_246
Trusted Contributor

for i in ????

Hi,

I have a question about the for i in statement.
How do i do a
for i in 1 2 3 4
do
..
done

but in a way of :

for i in [1-4]
do
..
done

And more important how do I do :

for i in [50-1]
do
..
done

I'dd like to calculate backwards with a large amount of numbers. Can anyone help ??

Regs David
@yourservice
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: for i in ????

Hi David:

Try this:

#!/usr/bin/sh
typeset -i N=10
while (( N > 0 ))
do
(( N=N-1 ))
echo ${N}
done

Regards!

...JRF...
John Meissner
Esteemed Contributor

Re: for i in ????

I think that the "for i" statement would not be your best bet to do the [50-1]

I would recommend using s structure like this:

count=50
while [ "$count" > "0" ]
do
........
done
All paths lead to destiny
John Meissner
Esteemed Contributor

Re: for i in ????

sorry - I hit send too quickly

I think that the "for i" statement would not be your best bet to do the [50-1]

I would recommend using s structure like this:

count=50
while [ "$count" > "0" ]
do
........
((count=$count-1))
done
All paths lead to destiny
David_246
Trusted Contributor

Re: for i in ????

Alright, so there is no "for i in ... " solution.
It must be done using a while loop.

Thanks for your help.

Regs David
@yourservice
Armin Kunaschik
Esteemed Contributor

Re: for i in ????

In Debian Linux there is a sec binary which is
used in some scripts... so why not write a
function that emulates sec:

function sec
{
typeset start=$1
typeset end=$2

while [[ $end -ge $start ]]
do
print $start
((start+=1))
done
}

Then use
for i in $(sec 1 50)
do
print $i
done

To practise a bit you can extend sec to
count down if $start is greater than $end
and add a bit error handling :-)
And now for something completely different...
Mike Stroyan
Honored Contributor

Re: for i in ????

This is one of the things i really like about /usr/dt/bin/dtksh. It is based on ksh93, which can do a real C style for loop.

for (( i=8; i>0; i-- )) ; do echo $i; done

You can read the manual on the ksh93 features by using "man -M /usr/dt/man ksh" to view /usr/dt/man/man1/ksh.1 .