source: vendor/bash/3.1-p17/examples/functions/seq2

Last change on this file was 3228, checked in by bird, 18 years ago

bash 3.1

File size: 814 bytes
Line 
1# Generate a sequence from m to n, m defaults to 1.
2
3seq ()
4{
5 declare -i lo hi i # makes local
6 local _SEQ INIT COMPARE STEP
7
8 case "$1" in
9 -r) INIT='i=$hi _SEQ=""' COMPARE='let "i >= $lo"' STEP='let i-=1' ; shift ;;
10 *) INIT='i=$lo _SEQ=""' COMPARE='let "i <= $hi"' STEP='let i+=1' ;;
11 esac
12
13 case $# in
14 1) lo=1 hi="$1" ;;
15 2) lo=$1 hi=$2 ;;
16 *) echo seq: usage: seq [-r] [low] high 1>&2 ; return 2 ;;
17 esac
18
19 # equivalent to the as-yet-unimplemented
20 # for (( "$INIT" ; "$COMPARE" ; "$STEP" )); do _SEQ="${_SEQ}$i "; done
21 eval "$INIT"
22 while eval "$COMPARE"; do
23 _SEQ="${_SEQ}$i "
24 eval "$STEP"
25 done
26 echo "${_SEQ# }"
27 return 0
28}
29
30# like the APL `iota' function (or at least how I remember it :-)
31iota()
32{
33 case $# in
34 1) seq 1 "$1"; return $?;;
35 *) echo "iota: usage: iota high" 1>&2; return 2;;
36 esac
37}
Note: See TracBrowser for help on using the repository browser.