-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprepare_dir_bak
More file actions
executable file
·80 lines (69 loc) · 2.21 KB
/
prepare_dir_bak
File metadata and controls
executable file
·80 lines (69 loc) · 2.21 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
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
#!/bin/bash
option_list=("TYPE" "DIR_NAME" "DO_BACKUP" "USE_EXISTING" "VERBOSE")
USAGE="Usage: $(basename ${0}) ${option_list[@]}
Prepare a directory and retain, remove, or back up a possible pre-existing directory of the same name.
Options:
- TYPE: Short description used in prints, e.g. \"backup\" or \"output\".
- DIR: Name of the directory.
- DO_BACKUP (true|false): Retain a pre-existing directory of the same name as '*.bak'.
- USE_EXISTING (true|false): Retain a pre-existing directory as-is, including its contents.
- VERBOSE (true|false): Print verbose output."
main()
{
# Eval inargs
local nargs_min=5
if [[ ${#} -lt ${nargs_min} ]]
then
echo "${USAGE}"
return 1
fi
local name="${1}"
local dirname="${2}"
local do_backup=${3}
local use_existing=${4}
local verbose=${5}
# Set verbosity flag for commands
[[ "${verbose}" == "true" ]] && vflag="-v" || vflag=""
# Handle pre-existing directory: skip, remove, or back it up
if [[ -d "${dirname}" ]]
then
# Use the existing directory
if ${use_existing}
then
if ${verbose}
then
echo "using existing ${name} directory: ${dirname}"
fi
return
fi
# Retain a backup-copy of the existing directory
if [[ "${do_backup}" == "true" ]]
then
bak="${dirname}.bak"
if ${verbose}
then
echo "warning: ${name} directory already exists: ${dirname}" >&2
echo "saving as ${bak}" >&2
fi
if [[ -d "${bak}" ]]
then
if ${verbose}
then
echo "warning: overwriting existing ${name} directory '${bak}'" >&2
fi
\rm -rf ${vflag} "${bak}"
fi
\mv -v "${dirname}" -T "${bak}"
# Remove the existing directory
else
if ${verbose}
then
echo "warning: overwriting existing ${name} directory '${dirname}'" >&2
fi
\rm -rf ${vflag} "${dirname}"
fi
fi
# Create the new directory
\mkdir -p ${vflag} "${dirname}"
}
main "${@}"