Operating System - HP-UX
1822498 Members
2616 Online
109642 Solutions
New Discussion юеВ

Re: simple: for i in 1 2 3 4 -----> 2000

 
SOLVED
Go to solution
Bill McNAMARA_1
Honored Contributor

simple: for i in 1 2 3 4 -----> 2000

loop until value less than 2000

best way!

addition:
if i % 16 is 0 (but not for i=0) echo "/n"

Thanks,
Bill

It works for me (tm)
10 REPLIES 10
Deepak Extross
Honored Contributor

Re: simple: for i in 1 2 3 4 -----> 2000

Bill,
for the loop, whats wrong with this:
i=0;
while [ $i -lt 2000 ]
do
echo $i
i=`expr $i + 1`
done
Mary Ann Lipa
Valued Contributor

Re: simple: for i in 1 2 3 4 -----> 2000

where can I start learning that kinda language?...

Thanks Bill..
d_b
Which is worse, smoking or picking your nose in a public place?
Robin Wakefield
Honored Contributor

Re: simple: for i in 1 2 3 4 -----> 2000

Hi Bill,

#!/bin/ksh
i=0
while [ $i -le 2000 ] ; do
echo $i '\c'
if [ `expr $i % 16` -eq 0 ] && [ $i -ne 0 ] ; then
echo
fi
i=`expr $i + 1`
done

Rgds, Robin.
Deepak Extross
Honored Contributor
Solution

Re: simple: for i in 1 2 3 4 -----> 2000

this one has the mod-16 bit in cluded:

i=0;
p=0;
while [ $i -lt 100 ]
do
echo $i
i=`expr $i + 1`
p=`expr $p + 1`
if [ $p -eq 16 ]
then
echo "\n"
p=0
fi
done

does this help?
Deepak Extross
Honored Contributor

Re: simple: for i in 1 2 3 4 -----> 2000

coming from someone crowned almost 4 times, is this some kind of trick question?
Karvendhan M
Frequent Advisor

Re: simple: for i in 1 2 3 4 -----> 2000

how is it?

# /usr/bin/perl
for (1 .. 2000) {
print <<`EOF`
echo $_
EOF
}

or....

for i in `perl -e 'for (1 .. 2000) { print "$_\n" }'`
do
echo $i
done



~ Karvendhan M.

Bill McNAMARA_1
Honored Contributor

Re: simple: for i in 1 2 3 4 -----> 2000

Thanks Deepak,

I ask because I've gotten _*real*_ lazy with the forums here!
I only got crowned once btw.. 4 wizard caps nearly! ;)

Later,
Bill
It works for me (tm)
Frank Slootweg
Honored Contributor

Re: simple: for i in 1 2 3 4 -----> 2000

Re:

i=`expr $i + 1`

If you have a Korn or POSIX shell script, then it is much more effective to use:

((i=$i+1))

The '((...))' construct is executed by the shell, while expr(1) is an external command, i.e. one process per iteration of the loop.

For the empty loop (2000 times), the time on my system went from 22 seconds to 0.2 seconds.
Craig Rants
Honored Contributor

Re: simple: for i in 1 2 3 4 -----> 2000

Bill,
Frank deserves a 10 for his contribution. I tested it and he was right on. I am going to change all my scripts to use that construct where applicable.

Kudos to Frank,
C
"In theory, there is no difference between theory and practice. But, in practice, there is. " Jan L.A. van de Snepscheut
David Lodge
Trusted Contributor

Re: simple: for i in 1 2 3 4 -----> 2000

My example:

typeset -Z4 i=1
while (( i < 2000 ))
do
print -n $i
(( i % 16 == 0 ))&& print
(( i += 1 ))
done

(the typeset is to neatly round make all numbers 4 digits)

I don't really think I could get it smaller without resorting to awk:
awk 'BEGIN {for (i=1;i<2000;i++) { printf "%s ",i; if (i % 16 == 0) print "" } }'

dave