seq(1)
This commit is contained in:
parent
d7eb2b3d43
commit
5048212260
72
bin/seq
72
bin/seq
@ -1,44 +1,52 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
|
# There are many bugs and limitations with this implementation as opposed to
|
||||||
|
# others, particularly NetBSD's. This should probably be rewritten in C.
|
||||||
|
|
||||||
argv0="$0"
|
argv0="$0"
|
||||||
|
|
||||||
nai() {
|
doublecheck() {
|
||||||
printf "$argv0: $1: Not an integer\n"
|
! str isdigit "$1" \
|
||||||
|
&& printf "%s: %s: Not an unsigned integer.\n" \
|
||||||
|
"$argv0" "$1" 1>&2 \
|
||||||
|
&& exit 1 \
|
||||||
|
|| return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
error() {
|
||||||
|
printf "%s: Internal error.\n"
|
||||||
|
"$argv0" 1>&2
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
usage() {
|
usage() {
|
||||||
printf "Usage:\n \
|
printf "\
|
||||||
$argv0 [last]
|
Usage:
|
||||||
$argv0 [first] [last]
|
%s [last]
|
||||||
$argv0 [first] [increment] [last]\n" \
|
%s [first] [last]
|
||||||
>/dev/stderr
|
%s [first] [increment] [last]
|
||||||
|
" "$argv0" "$argv0" "$argv0" 1>&2
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
[ -n "$1" ] || usage
|
test -n "$1" && test -z "$4" || usage
|
||||||
if [ -n "$3" ]; then
|
|
||||||
stris int "$3" \
|
test -n "$3" && doublecheck "$3"
|
||||||
|| nai "$3" \
|
test -n "$2" && doublecheck "$2"
|
||||||
&& last=$3
|
test -n "$1" && doublecheck "$1"
|
||||||
stris int "$2" \
|
|
||||||
|| nai "$2" \
|
# all args are guaranteed to be positive integers; no need to quote variables
|
||||||
&& increment=$2
|
|
||||||
stris int "$1" \
|
if test -n "$3"; then first=$1; increment=$2; last=$3
|
||||||
|| nai "$1" \
|
elif test -n "$2"; then first=$1; increment=1; last=$2
|
||||||
&& first=$1
|
else first=1; increment=1; last=$1
|
||||||
elif [ -n "$2" ]; then
|
|
||||||
stris int "$2" \
|
|
||||||
|| nai "$2" \
|
|
||||||
&& last=$2
|
|
||||||
stris int "$1" \
|
|
||||||
|| nai "$1" \
|
|
||||||
&& first=$1
|
|
||||||
increment=1
|
|
||||||
else
|
|
||||||
stris int "$1" \
|
|
||||||
|| nai "$1" \
|
|
||||||
&& last=$1
|
|
||||||
first=1
|
|
||||||
increment=1
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
|
i=$first
|
||||||
|
while true; do
|
||||||
|
printf "%s\n" $i
|
||||||
|
i=$(printf "%s %s + p\n" $i $increment | dc)
|
||||||
|
printf "%s %s - p\n" $last $i | dc | grep '\-' >/dev/null 2>&1 \
|
||||||
|
&& break \
|
||||||
|
|| continue
|
||||||
|
done
|
||||||
|
Loading…
Reference in New Issue
Block a user