Skip to content
This repository was archived by the owner on Jan 25, 2022. It is now read-only.

Commit bec7901

Browse files
committed
Added functions to_lowercase(), file_extension(), file_basename() and is_dry_run(); refactored next_backup_file() to use file_extension(); and refactored echo_if_not_quiet() to allow "\t" in messages.
1 parent 040d57b commit bec7901

File tree

1 file changed

+54
-8
lines changed

1 file changed

+54
-8
lines changed

guest/cli/includes/functions

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,44 @@
11
#!/usr/bin/env bash
22

3+
#
4+
# Outputs the file extension given a file name
5+
#
6+
# $1 - String
7+
#
8+
# Usage: string=$(to_lowercase "FOO.BAR")
9+
# Result: $string ==> "foo.bar"
10+
#
11+
#
12+
function to_lowercase() {
13+
echo "${1,,}"
14+
}
15+
16+
#
17+
# Outputs the file extension given a file name
18+
#
19+
# $1 - Filename
20+
#
21+
# Usage: extension=$(file_extension "foo.bar")
22+
# Result: $extension ==> "bar"
23+
#
24+
#
25+
function file_extension() {
26+
echo "${1##*.}"
27+
}
28+
29+
#
30+
# Outputs the base filename given a full filename
31+
# Base filename means no path and no extension
32+
#
33+
# $1 - Filename
34+
#
35+
# Usage: basename=$(file_basename "foo/bar.bar")
36+
# Result: $extension ==> "bar"
37+
#
38+
function file_basename() {
39+
filename=$(basename "$1")
40+
echo "${filename%.*}"
41+
}
342

443
#
544
# Returns a filename that does not exist given an existing filename.
@@ -21,8 +60,8 @@ function next_backup_file() {
2160
# Get the file extension if there is one
2261
# See: http://stackoverflow.com/a/965072/102699
2362
#
24-
ext=".${bakfile##*.}"
25-
if [[ "." == "${ext}" || "${ext}" == ".${bakfile}" ]]; then
63+
ext=$(file_extension "${bakfile}")
64+
if [[ "" == "${ext}" || "${ext}" == "${bakfile}" ]]; then
2665
ext=""
2766
fi
2867

@@ -37,15 +76,22 @@ function next_backup_file() {
3776
# Now check to see if the default prior backup exists
3877
# Continue checking until we find a workable filename
3978
#
40-
test="${bakfile}${ext}"
79+
test="${bakfile}.${ext}"
4180
until [ ! -e "${test}" ]; do
4281
counter=$((counter))
4382
let counter++
44-
test="${bakfile}${counter}${ext}"
83+
test="${bakfile}${counter}.${ext}"
4584
done
4685
echo "${test}"
4786
}
4887

88+
function is_dry_run() {
89+
if [[ "$1" == *"--dry-run"* ]]; then
90+
echo "true"
91+
return
92+
fi
93+
echo "false"
94+
}
4995

5096
function is_quiet() {
5197
if [[ "$1" == *"--quiet"* ]]; then
@@ -76,20 +122,20 @@ function echo_if_not_quiet() {
76122
message=${2:1}
77123
case "${control}" in
78124
"^")
79-
echo "${message}" | sed "${regex}"
125+
echo -e "${message}" | sed "${regex}"
80126
echo
81127
;;
82128
"*")
83129
echo
84-
echo "${message}" | sed "${regex}"
130+
echo -e "${message}" | sed "${regex}"
85131
echo
86132
;;
87133
"=")
88-
echo "${message}" | sed "${regex}"
134+
echo -e "${message}" | sed "${regex}"
89135
;;
90136
"&")
91137
echo
92-
echo "${message}" | sed "${regex}"
138+
echo -e "${message}" | sed "${regex}"
93139
;;
94140
*)
95141
echo

0 commit comments

Comments
 (0)