Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
att
GitHub Repository: att/ast
Path: blob/master/src/cmd/std/seq.sh
1808 views
1
########################################################################
2
# #
3
# This software is part of the ast package #
4
# Copyright (c) 1989-2012 AT&T Intellectual Property #
5
# and is licensed under the #
6
# Eclipse Public License, Version 1.0 #
7
# by AT&T Intellectual Property #
8
# #
9
# A copy of the License is available at #
10
# http://www.eclipse.org/org/documents/epl-v10.html #
11
# (with md5 checksum b35adb5213ca9657e911e9befb180842) #
12
# #
13
# Information and Software Systems Research #
14
# AT&T Research #
15
# Florham Park NJ #
16
# #
17
# Glenn Fowler <[email protected]> #
18
# #
19
########################################################################
20
#
21
# seq.sh
22
# Written by David Korn
23
# AT&T Labs
24
# Sat May 5 00:32:40 EDT 2007
25
#
26
case $(getopts '[-]' opt "--???man" 2>&1) in
27
version=[0-9]*)
28
usage=$'\n[-?\n@(#)$Id: seq (AT&T Labs Research) 2012-04-14 $\n]
29
'$USAGE_LICENSE$'
30
[+NAME?seq - print a sequence of numbers]
31
[+DESCRIPTION?\bseq\b writes the numbers from \afirst\a to \alast\a
32
in steps of increments. If \afirst\a or \aincr\a is omitted,
33
it defaults to 1. An omitted \aincr\a defaults to 1 even
34
when \alast\a is smaller than \afirst\a.]
35
[+?\afirst\a \aincr\a, and \alast\a are interpreted as floating
36
point values. \aincr\a is usually positive if \afirst\a is
37
smaller than \alast\a, and \aincr\a is usually negative
38
if \afirst\a is greater than \alast\a. When given, the
39
\aformat\a argument must contain exactly one of the
40
printf-style, floating point output formats \b%e\b, \b%f\b,
41
\b%g\b.]
42
[f:format]:[format:=%g?use printf style floating point \aformat\a.]
43
[s:separator]:[string:=\\n?use \astring\a to separate numbers.]
44
[w:equal-width?equalize width by padding with leading zeroes.]
45
46
[ first [ incr ] ] last
47
48
[+EXIT STATUS?]{
49
[+0?Success.]
50
[+>0?An error occurred.]
51
}
52
[+SEE ALSO?\bksh\b(1), \bprintf\b(3)]
53
'
54
;;
55
*)
56
usage=''
57
;;
58
esac
59
60
function err_exit
61
{
62
print -ru2 -- "$command: $@"
63
exit 1
64
}
65
66
function format # first second next_to_last last
67
{
68
typeset i
69
integer width=0 precision=0 n
70
for i
71
do n=${#i}
72
(( n > width )) && width=$n
73
[[ $i != *.* ]] && continue
74
i=${i##*.}
75
n=${#i}
76
(( n > precision )) && precision=$n
77
done
78
print -r -- "%0$width.${precision}f"
79
}
80
81
command=${0##*/}
82
width= sep=$'\n' fmt= end=
83
float first=1 incr=1 last n sign=1 epsilon
84
while getopts "$usage" var
85
do case $var in
86
f)
87
fmt=$OPTARG
88
[[ $fmt == *%*([^[:space:][:alpha:]])[efg]* ]] || err_exit "$fmt: invalid format string"
89
;;
90
s)
91
sep=$OPTARG;;
92
w)
93
width=1;;
94
esac
95
done
96
shift $((OPTIND-1))
97
case $# in
98
1) last=$1;;
99
2) first=$1 last=$2;;
100
3) first=$1 incr=$2 last=$3;;
101
0) err_exit "too few arguments";;
102
*) err_exit "too many arguments";;
103
esac
104
if [[ $width ]]
105
then [[ $fmt ]] && err_exit "format string may not be specified when printing equal width strings"
106
(( n = (last - first) / incr ))
107
if (( abs(floor(n) - n) <= 1e-12 ))
108
then end=$last
109
fi
110
fmt=$(format $first $((first+incr)) $((last-incr)) $end)
111
elif [[ ! $fmt ]]
112
then fmt=%g
113
fi
114
(( incr<0)) && sign=-1
115
(( epsilon = sign*incr*1e-12 ))
116
(( last += sign*epsilon ))
117
(( last *= sign ))
118
s=
119
for ((n=first; (sign*n) <= last; n+= incr ))
120
do printf "%s$fmt" "$s" $(( abs(n) > epsilon ? n : 0 ))
121
s=$sep
122
done
123
[[ $s ]] && print
124
exit 0
125
126