-
Notifications
You must be signed in to change notification settings - Fork 0
/
.functions
391 lines (332 loc) · 10.9 KB
/
.functions
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
function _display_error() {
local message="$1"
echo -e "\e[91mERROR:\e[0m $message"
}
function create_gzip_file() {
# create_gzip_file - Create a gzipped (.gz) version of a file
#
# This function takes the path to a file as an argument and creates a gzipped version
# of that file with the same name and a '.gz' extension. It performs error checking to
# ensure that the file exists, that a gzipped version doesn't already exist, and
# reports the outcome of the gzipping process.
#
# Usage:
# - create_gzip_file <file_path>
#
# Arguments:
# - file_path: The path to the file you want to gzip.
local file_path="$1"
local gzip_file="${file_path}.gz"
local original_size
if [ $# -ne 1 ]; then
_display_error "Usage: create_gzip_file <file_path>"
return 1
fi
if [ ! -e "$file_path" ]; then
_display_error "Source file '$file_path' not found."
return 1
fi
if [ -e "$gzip_file" ]; then
_display_error "Gzipped file already exists: $gzip_file"
return 1
fi
original_size=$(wc -c < "$file_path")
echo "Creating gzipped file: $gzip_file"
gzip -c "$file_path" > "$gzip_file"
if [ $? -eq 0 ]; then
# Get the gzipped file size
local gzipped_size
gzipped_size=$(wc -c < "$gzip_file")
echo "Original file size (bytes): $original_size"
echo "Gzipped file size (bytes): $gzipped_size"
echo "Gzipped file created successfully."
else
_display_error "Error creating gzipped file."
return 1
fi
}
function create_tar_gz_file() {
# create_tar_gz_file - Create a tar.gz archive from a directory
#
# This function creates a tar.gz archive from the specified source directory and
# saves it to the specified output file.
#
# Usage:
# create_tar_gz_file <source_directory> <output_file>
#
# Arguments:
# - <source_directory>: The source directory to archive.
# - <output_file>: The name of the output tar.gz file to create.
#
# Example usage:
# create_tar_gz_file ~/my_project /tmp/my_project.tar.gz
local source_dir="$1"
local output_file="$2"
if [ -z "$source_dir" ] || [ -z "$output_file" ]; then
_display_error "Usage: create_tar_gz <source_directory> <output_file>"
return 1
fi
if [ ! -d "$source_dir" ]; then
_display_error "Source directory '$source_dir' not found."
return 1
fi
echo "Creating tar.gz archive from '$source_dir'..."
original_size=$(du -sh "$source_dir" | awk '{print $1}')
if tar czf "$output_file" -C "$source_dir" .; then
compressed_size=$(du -sh "$output_file" | awk '{print $1}')
echo "Tar.gz archive created successfully: $output_file"
echo "Original size: $original_size"
echo "Compressed size: $compressed_size"
return 0
else
_display_error "Error creating tar.gz archive."
return 1
fi
}
function get_file_sizes() {
# file_sizes - Display the sizes of files and directories in the current directory.
#
# This function uses the 'du' command to calculate and display the sizes of files
# and directories in the current directory. It can also display sizes in human-
# readable format with options to handle different file sizes.
#
# Usage:
# - To display sizes of files and directories in the current directory:
# file_sizes
# - To display the size of a specific file or directory:
# file_sizes <file_or_directory> [<file_or_directory2> ...]
#
# Arguments:
# - file_or_directory: Optional. The path(s) to the file(s) or directory(ies)
# you want to display the size of. If no argument is
# provided, sizes for all files and directories in the
# current directory are displayed.
local arg="-sh"
if du -b /dev/null > /dev/null 2>&1; then
arg="-sbh" # Use human-readable sizes
fi
local target="*"
if [[ -n "$1" ]]; then
target=("$@")
fi
du "$arg" -- "${target[@]}"
}
function get_gzip_size() {
# create_gzip_file - Create a gzipped (.gz) version of a file
#
# This function takes the path to a file as an argument and creates a gzipped version
# of that file with the same name and a '.gz' extension. It performs error checking to
# ensure that the file exists, that a gzipped version doesn't already exist, and
# reports the outcome of the gzipping process.
#
# Usage:
# create_gzip_file <file_path>
#
# Arguments:
# - <file_path>: The path to the file you want to gzip.
local original_size
local gzipped_size
if [ -z "$1" ]; then
_display_error "No input file provided."
return 1
fi
if ! [ -f "$1" ]; then
_display_error "File '$1' not found."
return 1
fi
original_size=$(wc -c < "$1")
gzipped_size=$(gzip -c "$1" | wc -c)
echo "Original size (bytes): $original_size"
echo "Gzipped size (bytes): $gzipped_size"
}
function get_local_ips() {
# get_local_ips: Retrieve all local IP addresses of the current machine.
#
# This function uses the 'ip' command to extract all local IP addresses (IPv4
# and IPv6) configured on the machine, excluding loopback addresses.
#
# Usage:
# - local_ips=($(get_local_ips))
#
# Returns:
# An array containing all local IP addresses (IPv4 and IPv6).
local LOCAL_IPS
LOCAL_IPS=($(ifconfig | grep "inet " | grep -v 127.0.0.1 | awk '{print $2}'))
if [ ${#LOCAL_IPS[@]} -eq 0 ]; then
_display_error "No local IP addresses found."
else
for ip in "${LOCAL_IPS[@]}"; do
echo "Local IP address: $ip"
done
fi
}
function get_public_ip() {
# my_ip: Retrieve the public and local IP addresses of the current machine.
# This function uses 'curl' to fetch the public IP from 'icanhazip.com',
# and 'ip addr' to extract the local IP addresses, filtering out loopback
# and IPv6 addresses.
#
# Usage:
# - public_ip=$(my_ip) # Get the public IP address
#
# Returns:
# Public IP address when called without arguments, or an array of local
# IP addresses when called with 'local' as an argument.
local PUBLIC_IP
PUBLIC_IP=$(wget -qO- --secure-protocol=auto https://icanhazip.com 2>/dev/null)
if [ $? -eq 0 ]; then
if [ -n "$PUBLIC_IP" ]; then
echo "Public IP address: $PUBLIC_IP"
else
_display_error "Empty response from the server."
fi
else
_display_error "Failed to retrieve the public IP address."
fi
}
function get_whois_info() {
# get_whois_info - Retrieve WHOIS information for a domain or URL
#
# This function checks if the 'whois' command is available, extracts the domain from a provided URL,
# and fetches WHOIS information using 'whois.internic.net'. It also handles error cases such as
# missing 'whois' command, invalid queries, and unavailable domain records.
#
# Usage:
# get_whois_info <domain_or_url>
#
# Arguments:
# - <domain_or_url>: The domain or URL for which WHOIS information is to be retrieved.
if ! command -v whois &> /dev/null; then
_display_error "'whois' command not found. Please install 'whois' to use this function."
return 1
fi
local domain
if [ -z "$1" ]; then
_display_error "No domain or URL provided."
return 1
else
domain=$(echo "$1" | awk -F/ '{print $3}')
if [ -z "$domain" ]; then
domain="$1"
fi
fi
echo "Getting whois record for: $domain ..."
if result=$(/usr/bin/whois -h whois.internic.net "$domain" 2>&1); then
if echo "$result" | grep -qi "No match for domain"; then
_display_error "No match found for domain: $domain"
return 1
elif echo "$result" | grep -qi "invalid query"; then
_display_error "Invalid query for domain: $domain"
return 1
else
echo "$result" | sed '/NOTICE:/q'
fi
else
_display_error "Unable to retrieve whois information for domain: $domain"
return 1
fi
}
function get_gzip_size() {
# create_gzip_file - Create a gzipped (.gz) version of a file
#
# This function takes the path to a file as an argument and creates a gzipped version
# of that file with the same name and a '.gz' extension. It performs error checking to
# ensure that the file exists, that a gzipped version doesn't already exist, and
# reports the outcome of the gzipping process.
#
# Usage:
# create_gzip_file <file_path>
#
# Arguments:
# - <file_path>: The path to the file you want to gzip.
local original_size
local gzipped_size
if [ -z "$1" ]; then
_display_error "No input file provided."
return 1
fi
if ! [ -f "$1" ]; then
_display_error "File '$1' not found."
return 1
fi
original_size=$(wc -c < "$1")
gzipped_size=$(gzip -c "$1" | wc -c)
echo "Original size (bytes): $original_size"
echo "Gzipped size (bytes): $gzipped_size"
}
function mk_dir() {
# mk_dir: Create a directory and change to it.
#
# This function creates a directory with 'mkdir -p' and then changes to it using 'cd'.
# It checks if the directory creation was successful before attempting to change to it.
#
# Usage:
# - mk_dir directory_path
#
# Parameters:
# directory_path: The path to the directory to be created and changed to.
local DIRECTORY_PATH="$1"
if mkdir -p "$DIRECTORY_PATH"; then
cd "$DIRECTORY_PATH" || return 1
else
return 1
fi
}
function mk_tempd() {
# mk_tempd: Create a temporary directory and change the current working directory to it.
#
# This function generates a temporary directory using 'mktemp' and optionally
# appends a provided prefix to the directory name. It then changes the current
# working directory to the newly created directory.
#
# Usage:
# - Without arguments: Create a temporary directory with a random name.
# - With a prefix argument: Create a temporary directory with the given prefix.
#
# Parameters:
# prefix: Create a temporary directory with the given prefix.
local DIR
if [ $# -eq 0 ]; then
DIR=$(mktemp -d)
else
DIR=$(mktemp -d -t "${1}.XXXXXXXXXX")
fi
cd "$DIR" || exit
}
function pb_copy() {
# pb_copy - Copy content from standard input to the clipboard.
#
# This function checks if 'pbcopy' is available and uses it to copy
# the content to the clipboard. If 'pbcopy' is not available, it
# falls back to using 'xclip'. Useful for easily copying content
# to the clipboard in a cross-platform manner.
#
# Usage:
# echo "Hello, clipboard!" | pb_copy
#
stdin=$(</dev/stdin);
pbcopy="$(which pbcopy)";
if [[ -n "$pbcopy" ]]; then
echo "$stdin" | "$pbcopy"
else
echo "$stdin" | xclip -selection clipboard
fi
}
function pb_paste() {
# pb_paste - Paste content from the clipboard to standard output.
#
# This function checks if 'pbpaste' is available and uses it to paste
# the content from the clipboard to standard output. If 'pbpaste' is
# not available, it falls back to using 'xclip'. Useful for easily
# pasting content from the clipboard in a cross-platform manner.
#
# Example usage:
# pb_paste > output.txt
#
pbpaste="$(which pbpaste)";
if [[ -n "$pbpaste" ]]; then
"$pbpaste"
else
xclip -selection clipboard
fi
}