-
Notifications
You must be signed in to change notification settings - Fork 0
/
repchar.bash
executable file
·91 lines (68 loc) · 2.59 KB
/
repchar.bash
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/bin/bash
# vim:set ts=4 sw=4 tw=80 et ai si:
# ( settings from: http://datapax.com.au/code_conventions/ )
#
#/**********************************************************************
# Rep(eat) Char(acter)
# Copyright (C) 2013-2017 Todd Harbour
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 ONLY, as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program, in the file COPYING or COPYING.txt; if
# not, see http://www.gnu.org/licenses/ , or write to:
# The Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# **********************************************************************/
# repchar
#--------
# Repeats a character or series of characters. Can be sourced or run.
# We are being run as a script, so set it's name
[ "${0}" == "${BASH_SOURCE}" ] && _repcharbinname="${0##*/}"
# repchar -h|--help
# repchar <char> <count>
function repchar() {
[ "${1}" == "--help" ] || [ "${1}" == "-h" ] && {
cat <<EOF
Repeats a given character (or string) a specified number of times.
Can be run directly or sourced ( . "${_repcharbinname:-repchar}" ).
Usage: ${_repcharbinname:-repchar} -h|--help
${_repcharbinname:-repchar} <char> <count>
-h|--help - Displays this help
<char> - Character (or string) to repeat.
<count> - Number of times to repeat <char>.
Example: ${_repcharbinname:-repchar} = 10
=========
EOF
return 0
}
# Expecting 2 params
[ ${#} -ne 2 ] && {
echo >&2 "ERROR: Invalid number of parameters"
return 1
}
# If first param is NULL, obviously the output will be nothing
[ -z "${1}" ] && echo && return 0
# Second param must be a number
[ "${2}" -eq "${2}" ] &>/dev/null || {
echo >&2 "ERROR: Invalid <num>: ${2}"
return 1
}
# Raw that number (strip of extras)
num=$((${2} + 0))
# If second param is a zero, obviously the output will be nothing
[ "${num}" -eq 0 ] && echo && return 0
printf "%.0s${1}" $(eval echo "{1..${num}}")
echo
} # repchar
# We are being sourced
[ "${0}" != "${BASH_SOURCE}" ] && return 0
# We are being run as a script
repchar "${@}"; exit $?