-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.sh
More file actions
50 lines (47 loc) · 1.43 KB
/
Copy pathtemp.sh
File metadata and controls
50 lines (47 loc) · 1.43 KB
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
#!/bin/bash
temp_dir() {
local -r dirpath="$(mktemp -d)"
if [[ ! "$dirpath" || ! -d "$dirpath" ]]; then
if declare -f -F log_error >/dev/null; then
log_error "Could not create temporary directory."
else
printf "Could not create temporary directory.\n"
fi
return 1
fi
declare -f -F log_debug >/dev/null \
&& log_debug "Created temp dir %s" "$dirpath"
echo "${dirpath}"
}
track_temp_dir() {
dirpath="$1"
if [[ ! "$dirpath" || ! -d "$dirpath" ]]; then
if declare -f -F log_error >/dev/null; then
log_error "%s is not a dir" "$dirpath"
else
printf "%s is not a dir\n" "$dirpath"
fi
return 1
fi
if [[ -z ${__TEMP_DIR_LIST__+x} ]]; then
declare -ag __TEMP_DIR_LIST__=("$dirpath")
trap temp_dir_cleanup EXIT
else
__TEMP_DIR_LIST__+=("$dirpath")
fi
}
temp_dir_cleanup() {
for dirpath in "${__TEMP_DIR_LIST__[@]}"; do
if ! rm -rf "$dirpath"; then
if declare -f -F log_critical >/dev/null; then
log_critical "Could not delete directory %s. Aborting." "$dirpath"
else
printf "Could not delete directory %s. Aborting.\n" "$dirpath"
fi
else
if declare -f -F log_debug >/dev/null; then
log_debug "Deleted temp dir %s" "$dirpath"
fi
fi
done
}