-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumberCount.sh
48 lines (41 loc) · 882 Bytes
/
numberCount.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#/bin/bash
# This script prints a range of numbers
# Usage: count [-r] [-b n] [-s n] stop
# -b gives the number to begin with (default: 0)
# -r reverses the count
# -s sets step size (default: 1)
# counting will stop at stop.
declare reverse=""
declare -i begin=0
declare -i step=1
while getopts "b:s:r" opt; do
case $opt in
r)
reverse="yes"
;;
b)
[[ ${OPTARG} =~ ^[0-9]+$ ]] || { echo "${OPTARG} is not a number" >&2; exit 1; }
start="${OPTARG}"
;;
s)
[[ ${OPTARG} =~ ^[0-9]+$ ]] || { echo "${OPTARG} is not a number" >&2; exit 1; }
step="${OPTARG}"
;;
\?)
exit 1
;;
esac
done
shift $(( OPTIND -1 ))
[[ $1 ]] || { echo "missing an argument" >&2; exit 1; }
declare end="$1"
if [[ ! $reverse ]]; then
for (( i=start; i <= end; i+=step )); do
echo $i
done
else
for (( i=end; i >= start; i-=step )); do
echo $i
done
fi
exit 0