-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommonlib.sh
executable file
·154 lines (137 loc) · 3.7 KB
/
commonlib.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/bin/bash
# Make sure nothing is done in case the library is sourced more than once.
if [ -n "$__COMMONLIB_SOURCED__" ]; then
return
else
__COMMONLIB_SOURCED__=1
fi
#######################################
# Print provided text to stderr.
# Globals:
# None
# Arguments:
# Text to print
# Outputs:
# The text -> stderr
#######################################
common::print_to_stderr() (
text_to_print="${1:?[!] The text to print is not specified}"
printf '%b' "${text_to_print}" >&2
)
#######################################
# Changes directory and exits on fail.
# Globals:
# None
# Arguments:
# Directory to enter
# Outputs:
# None
#######################################
common::safe_cd() {
local target_dir="${1:?[!] Target directory is not specified}"
cd "${target_dir}" || {
common::print_to_stderr "[!] Cannot cd to ${target_dir}"
exit 1
}
}
#######################################
# Extract an archive to destination directory.
# Globals:
# None
# Arguments:
# Archive to extract, absolute path
# Destintaion directory, absolute path
# Outputs:
# None
#######################################
common::extract() (
archive="${1:?[!] Path to the archive is not specified}"
destination="${2:?[!] Destination path is not specified}"
if [ ! -d "$destination" ]; then
mkdir -p "$destination"
fi
if [ -f "$archive" ]; then
case "$archive" in
*.tar.bz2) tar xjf "$archive" -C "$destination" --strip-components 1 ;;
*.tar.gz) tar xzf "$archive" -C "$destination" --strip-components 1 ;;
*.tar.xz) tar xvfJ "$archive" -C "$destination" --strip-components 1 ;;
*.tar) tar xf "$archive" -C "$destination" --strip-components 1 ;;
*.tbz2) tar xjf "$archive" -C "$destination" --strip-components 1 ;;
*.tgz) tar xzf "$archive" -C "$destination" --strip-components 1 ;;
*) common::print_to_stderr "[!] '${archive}' cannot be extracted with extract()"; return 1 ;;
esac
else
common::print_to_stderr "[!] '${archive}' is not a valid file"
return 1
fi
)
#######################################
# Remove leading and trailing whitespaces.
# Globals:
# None
# Arguments:
# Text to trim
# Outputs:
# Trimmed text -> stdout
#######################################
common::trim() (
text_to_trim="$*"
echo "${text_to_trim}" | sed 's/^ *//g' | sed 's/ *$//g'
)
#######################################
# Append line to a string.
# Globals:
# None
# Arguments:
# Line to append
# Text to append to (may be empty)
# Outputs:
# Text with appended line -> stdout
#######################################
common::append_line() (
line="${1:?[1] Line is not specified}"
# May be empty.
text="${2}"
if [ -z "${text}" ]; then
printf '%s' "${line}"
else
printf '%s\n%s' "${text}" "${line}"
fi
)
#######################################
# Check if a word is in a list delimited by spaces.
# Globals:
# None
# Arguments:
# Word (e.g. "dog")
# List (e.g. "cat dog mouse")
# Outputs:
# None
#######################################
common::is_word_in_spaced_list() (
searched_word="${1:?[!] Word is not specified}"
list="${2:?[!] List is not specified}"
# Won't need to restore IFS - running in a subshell
IFS=' '
set -f
for word in ${list}; do
if [ "${word}" = "${searched_word}" ]; then
return 0
fi
done
return 1
)
#######################################
# Create temporary directory.
# Globals:
# None
# Arguments:
# Base directory (absolute path?)
# Outputs:
# Created temp directory (absolute path) -> stdout
#######################################
common::create_temp_dir() (
base_dir="${1}"
tmp_dir="$(mktemp -dt -p "${base_dir}" tmpdir.XXXXXX)"
echo "${tmp_dir}"
)