-
Notifications
You must be signed in to change notification settings - Fork 0
/
repeat
executable file
·67 lines (53 loc) · 1.58 KB
/
repeat
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/bin/bash -e
# Activate debug mode
if [[ "$DEBUG" ]]; then
PS4='L${LINENO}: '
set -x
fi
# ----------------------------------------------------------------------------
OPTS=$(getopt -o c:w:hv --long character:,width:,help,version \
-n "${0##*/}" -- "$@") \
|| { echo "Terminating..." >&2; exit 1; }
eval set -- "$OPTS"
version(){
cat<<EOF
${0##*/} 2019.06.1
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Written by Yaswant Pradhan.
EOF
}
usage(){
cat<<EOF
${0##*/} Repeat a character (or string) n times.
Usage: ${0##*/} [OPTION]
Options:
-c, --character[=VALUE] char/string to repeat (default '-')
-n, --wide[=VALUE] number of times to repeat (default 80)
-h, --help print this help and exit
-v, --version print version and exit
Examples:
repeat x 80
repeat 'abc ' 10
repeat -c '\/' -n 5
Report bugs to <[email protected]>
EOF
}
# ----------------------------------------------------------------------------
while true; do
case "$1" in
-c |--character) c="$2"; shift 2 ;;
-w |--width) n="$2"; shift 2 ;;
-h |--help ) usage; exit 0 ;;
-v |--version ) version; exit 0 ;;
-- ) shift; break ;;
* ) break ;;
esac
done
c=${c:-${1:-'-'}}
n=${n:-${2:-80}}
# single character repeat
# printf "%${n}s\n" | tr " " "$c"
# multi-character repeat
str=$(printf "%${n}s")
printf "%s\n" "${str// /$c}"