for loops
Daniel Pfeiffer
pfeiffer at cix.cict.fr
Tue Apr 16 23:16:47 AEST 1991
What I don't like about the expr example, is the number of times
we need to fork, since it's not a builtin. The following, adapted
from a proposal one or two months back is a bit hard to understand,
(documented at the end) but should be lots quicker:
$ FILES=5
$ for i in `echo "[p 1 + d $FILES!<P] sP 1 lP x" | dc`
> do
> echo $i
> done
1
2
3
4
5
$
Someone also proposed a comparable script for awk, but, given that awk
is a rather huge language, I suppose that dc is quicker. Alternately,
for better readability, you might say
count() {
echo "[p 1 + d $1 !<P] sP 1 lP x" | dc
}
for i in `count 5`
You can also twiddle the first 1 to change the increment, or the
second 1 to change the starting point, as in the following script:
#! /bin/sh
if [ ! "$1" ]; then
echo "usage: count number
count beg end [step]">&2
exit 1
elif [ $# = 1 ]; then
set 1 $1
fi
# Program [ print top, increment top, duplicate, push end, pop and compare
# two top values, reexecute P if not past end ]
# store program as P, push beginning, load P, execute it
echo "[p ${3-1} + d $2 !<P] sP $1 lP x" | dc
--
-- Daniel Pfeiffer <pfeiffer at cix.cict.fr>
-- Tolosa (Toulouse), Midi-Pyrenees, Europe <pfeiffer at irit.fr>
-- "Beware - polyglot esperantist" <pfeiffer at frcict81.bitnet>
--
N
_---_
/ \ NEWS, it goes around the world.
W (-------) E (sorry, my bitmap doesn't have a world-class resolution)
\_ _/
---
More information about the Comp.unix.shell
mailing list