-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunc.sh
More file actions
executable file
·307 lines (266 loc) · 6.76 KB
/
Copy pathfunc.sh
File metadata and controls
executable file
·307 lines (266 loc) · 6.76 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
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
#!/bin/bash
# A library of utility functions for bash scripting.
err_info() {
echo -n "${BASH_SOURCE[$((${#BASH_SOURCE[@]} - 1))]##*/}: "
for (( i=0; i < $(( ${#FUNCNAME[@]} - 1 )); i++ )); do
echo -n "${FUNCNAME[$i]}(${BASH_LINENO[$i]}): "
done
echo "$*"
}
# Prints error info, then returns exit status of 1.
# @param additional error message
# @returns exit status
err_exit() {
echo "$(err_info $*)" >&2
exit 1
}
# Checks the exit status is 0 for previous command, else err_exit
# @returns exit status
check_for_zero_exit_status() {
if (( "$?" != 0 )); then
return "$?"
fi
}
# Sources specifed file or returns
# @param file to source
source_or_return()
{
local file="$1"
if [[ -f "$file" ]]; then
. "$file"
else
err_info "Could not source ${file##*/}" && return 1
fi
}
# Sources specified file or exits.
# @param file to source
source_or_exit() {
local file="$1"
if [[ -f "$file" ]]; then
. "$file" "${@:2}"
else
err_exit "Could not source ${file##*/}"
fi
}
# Checks parameters given to script. Looks for one argument to be specified (or exits).
# @param the script's parameters
# TODO: add more functions for checking number of args, options vs tokens vs option w/ token
specify_arg() {
if (( $# < 1 )); then
err_info "Please specify arg" && return 1
fi
}
# First checks number of required parameters. Then checks if "--help" or "-h" are provided as
# options to the script.
# @returns err_exit if number of params are less than provided.
# @returns export $helpOpt boolean
check_args() {
local n="$1" # number of required params
if (( $# < $(( $n + 1 )) )); then
err_exit "A minimum of $n arg(s) are required ($#)"
fi
local reqParams=("${@:2:$n}")
local i=0
for (( i=0; i<${#reqParams[@]}; i++)); do
if [[ -z "${reqParams[$i]}" ]]; then
err_exit "specify arg: $(( i + 1 ))"
fi
done
}
# TODO: update
positional_param1() {
if [[ "$1" != -* ]]; then
err_exit "First arg is a positional parameter"
fi
}
# Checks the given path-to-directory exists.
# @param path-to-directory
# @returns exit status
check_path_exists() {
local path="$1"
if ! [[ -d "$path" ]] || [[ -z "$path" ]]; then
return 1
fi
}
# Checks the given path-to-directory exists.
# @param path-to-directory
# @returns exit status
check_file_exists() {
local file="$1"
if ! [[ -e "$file" ]] || [[ -z "$file" ]]; then
return 1
fi
}
# Prompts terminal for continuation response
# @input from user
# @returns exit status
continue_prompt() {
echo -en "[PROMPT] Do you want to continue? (y/n)\t"
read continue
if [[ "$continue" == "y" ]]; then
return 0
elif [[ "$continue" == "n" ]]; then
return 1
else
echo "Please respond with 'y' or 'n'."
continue_prompt
fi
}
# Prints array element per line.
# @param a bash array
arr_cmd() {
local cmd="$1"
local arr=( "${@:2}" )
for el in "${arr[@]}"; do
eval $cmd "$el"
done
}
arr_uniq() {
local pattern="$1"
local arr=("${@:2}")
if (( ${#arr[@]} > 0 )); then
for el in "${arr[@]}"; do
if [[ "$el" == "$pattern" ]]; then
echo true && return 0
fi
done
else
return 1
fi
echo false && return 0
}
# Function to find the most frequent string in an array
arr_most_freq_str() {
local arr=("$@")
declare -A count
local maxCount=0
export maxString=""
local -a maxStrings
export isTied=false
# Count occurrences and track strings with maximum count
for str in "${arr[@]}"; do
((count["$str"]++))
if [ ${count["$str"]} -gt $maxCount ]; then
maxCount=${count["$str"]}
maxString="$str"
maxStrings=("$str")
elif [ ${count["$str"]} -eq $maxCount ]; then
maxStrings+=("$str")
fi
done
# Handle empty array
if [ $maxCount -eq 0 ]; then
return 1
fi
# Set isTied: true if multiple strings have max count or all counts are 1
if [ ${#maxStrings[@]} -gt 1 ] || [ $maxCount -eq 1 ]; then
export isTied=true
fi
return 0
}
# TODO: update
mv_check() {
# Only supports SOURCE to DEST (no -t option)
local source="$1"
local dest="$2"
if (( $# != 2 )); then
err_exit "specify 2 arguments"
fi
if ! readlink -e "$source" > /dev/null; then
err_info "$source doesn't exist" && return 1
fi
if [[ -d "$dest" ]]; then
echo "Moves $source in $dest directory"
else
echo "Renames $source to $dest"
fi
}
# Copies given array of files from source directory to given destination.
# @param source (directory)
# @param dest (directory)
# @param array of files to be copied
copy_files_array_to_dest() {
local exit_status=0;
local source="$1"
local dest="$2";
local array=("${@:3}")
for file in "${array[@]}"; do
cp -fv $source/$file $dest
wait
done
}
clean_directory() {
local flagQuiet=false
while (( $# > 0 )); do
case "$1" in
-q)
flagQuiet=true
shift
;;
*)
break
;;
-*)
err_exit "${FUNCNAME[0]}(): Unknown option: $1"
;;
esac
done
local dirPath="$1"
check_args 1 "$dirPath"
# Validate input
if [[ -z "$dirPath" || "$dirPath" == "/" || \
"$dirPath" == "/home" || "$dirPath" == "/etc" ]]; then
err_exit "Invalid or dangerous path: $dirPath"
fi
# Check if path exists
check_path_exists "$dirPath" || err_exit "Path doesn't exist: $dirPath"
# Check if there are files or subdirectories to delete
if compgen -G "$dirPath/*" > /dev/null; then
$flagQuiet || echo "Cleaning directory: $dirPath"
rm -rf "$dirPath/"* || err_exit "Failed cleaning $dirPath/*: $dirPath/*"
$flagQuiet || echo "Deleted contents of $dirPath"
fi
}
# Helper function for selecting from an array.
make_selection() {
local array=();
array=( "$@" );
select selection in "${array[@]}" "DONE"; do
if (( 1 <= "$REPLY" )) && \
(( "$REPLY" <= ${#array[@]} )); then
break
elif (( "$REPLY" == ${#array[@]} + 1 )); then
break
else
echo "Select any number from 1-$(( ${#array[@]} + 1 ))"
fi
done
export REPLY selection
}
# Prints recursively, given file's '# TODO:' comments in org-mode format
org_grep_todos() {
local target="$1"
grep -R -H -n -E '^[[:space:]]*# TODO:' "$target" |
while IFS=: read -r file line rest; do
todo=$(printf '%s\n' "$rest" |
sed -E 's/^[[:space:]]*# TODO:[[:space:]]*//')
printf '** TODO %s\n' "$todo"
printf ' [[file:%s::%s][%s:%s]]\n' \
"$file" "$line" "$(basename "$file")" "$line"
done
}
# Adds non-duplicate path to `dirs` builtin directory stack (suppresses normal change of directory)
add_new_path_to_dir_stack() {
local path="$1"
check_args 1 "$path"
pathFound=false
for dir in $(dirs); do
if [[ "$dir" == "$path" ]]; then
pathFound=true;
break
fi
done
if ! $pathFound; then
pushd -n "$path" > /dev/null
fi
}