waypoint
This commit is contained in:
1 parent
61e12cd0d5
commit
0c24400530
1 file changed
+139
-37
+139
-37
@@ -1,42 +1,59 @@
|
||||
#!/bin/bash
|
||||
# break all args into a string
|
||||
# all_args="$@"
|
||||
# echo "args: $all_args"
|
||||
# echo "arg number: $#"
|
||||
# for arg in "$@"; do
|
||||
#
|
||||
# done
|
||||
while getopts "w:b:pdc:" opt; do
|
||||
set -e
|
||||
|
||||
#initalize globals
|
||||
## prepended space to make final assembly cleaner
|
||||
whitelist_find_str=' \('
|
||||
blacklist_find_str=' -not \('
|
||||
search_dir="$PWD"
|
||||
whitelist=()
|
||||
blacklist=()
|
||||
ending=''
|
||||
action=' -print'
|
||||
|
||||
help_use () {
|
||||
echo "Usage:"
|
||||
echo -e '\tfind_ext.sh [-w <str whitelisted extentions>] [-b <str blacklisted extensions>] [-[-p] [-c <str path>] [-d]'
|
||||
echo -e '\t\t-w "list of file extensions" - Whitelist: Match files with provided extensions'
|
||||
echo -e '\t\t\texample: find_ext.sh -w "txt pdf doc"'
|
||||
}
|
||||
|
||||
# use getopts to parse options
|
||||
## each letter is a flag like -p
|
||||
## letter with : after is is a flag with an arg like -w "txt pdf gif"
|
||||
## first : is for when no flags are added (defaults, error handling)
|
||||
while getopts ":w:b:d:c:Dhps]" opt; do
|
||||
case "$opt" in
|
||||
# directory - path to search in (default $PWD)
|
||||
## find_ext.sh -w "txt jpg" -d /home/princesspi
|
||||
d)
|
||||
search_dir="${OPTARG[@]}"
|
||||
|
||||
if [ ! -d "$search_dir" ]; then
|
||||
echo "Error: Search directory $search_dir not found" >&2
|
||||
help_useage
|
||||
exit 1
|
||||
fi
|
||||
;;
|
||||
|
||||
# silent
|
||||
## find_ext -w "txt mp3" -s
|
||||
s)
|
||||
# show error text but silence everything else
|
||||
ending=' 1>/dev/null'
|
||||
;;
|
||||
|
||||
# whitelist - search for files ending in provided extensions
|
||||
## find_ext.sh -w "txt pdf bak" -p
|
||||
w)
|
||||
## break whitelist str into array
|
||||
whitelist_array="${OPTARG[@]}"
|
||||
echo "$whitelist_array"
|
||||
IFS=' ' read -r -a whitelist <<< "$whitelist_array"
|
||||
# echo $whitelist
|
||||
|
||||
# echo "opt"
|
||||
# echo "$opt"
|
||||
# declare -p opt
|
||||
# echo
|
||||
# echo "OPTARG"
|
||||
# echo "$OPTARG"
|
||||
# declare -p OPTARG
|
||||
# echo "whitelist_array"
|
||||
# echo "$whitelist_array"
|
||||
# declare -p whitelist_array
|
||||
# echo
|
||||
# echo "whitelist"
|
||||
# echo "$whitelist"
|
||||
# declare -p whitelist
|
||||
IFS=' ' read -r -a whitelist <<< "${OPTARG[@]}"
|
||||
|
||||
### start the whitelist group
|
||||
#### prepended space to make final assembly cleaner
|
||||
whitelist_find_str=' \('
|
||||
#### whitelist counter so it knows its in the first position (to not add the -o)
|
||||
whitelist_counter=0
|
||||
### loop through array of whitelisted exts
|
||||
for ext in "${whitelist}"; do
|
||||
for ext in "${whitelist[@]}"; do
|
||||
### only add -o after the first one
|
||||
if [ $whitelist_counter -eq 0 ]; then
|
||||
#### test if array has exactly one item
|
||||
@@ -58,9 +75,100 @@ while getopts "w:b:pdc:" opt; do
|
||||
### appended space to make final assembly cleaner
|
||||
whitelist_find_str+='\) '
|
||||
;;
|
||||
|
||||
# blacklist - exclude provided extensions from search
|
||||
## find_ext.sh -b "txt pdf jpg" -p
|
||||
b)
|
||||
# break blacklist str into array
|
||||
IFS=' ' read -r -a blacklist <<< "${OPTARG[@]}"
|
||||
echo ${#blacklist[@]}
|
||||
# create the blacklist find section text
|
||||
## start the whitelist group
|
||||
## has the -not for blacklisting
|
||||
## whitelist counter so it knows its in the first position (to not add the -o)
|
||||
blacklist_counter=0
|
||||
## loop through array of whitelisted exts
|
||||
for ext in "${blacklist[@]}"; do
|
||||
### test if array has exactly one item
|
||||
if [ ${#blacklist[@]} -eq 1 ]; then
|
||||
#### Append space because its the only entry
|
||||
blacklist_find_str+=" -iname \"*.${ext}\" "
|
||||
else
|
||||
#### no appended space on first one to make it neat with the -o's
|
||||
blacklist_find_str+=" -iname \"*.${ext}\""
|
||||
fi
|
||||
|
||||
### increment counter
|
||||
blacklist_counter=$(($whitelist_counter + 1));
|
||||
done
|
||||
## finish the whitelist group
|
||||
### appended space to make final assembly cleaner
|
||||
blacklist_find_str+=' \) '
|
||||
;;
|
||||
|
||||
# print (default(
|
||||
## find_ext.sh "jpg gif png" -p
|
||||
p)
|
||||
# do nothing here, just do the default print silently
|
||||
# action=' -print'
|
||||
;;
|
||||
|
||||
# delete
|
||||
## find_ext.sh -w "tmp bak" -d
|
||||
D)
|
||||
# verify that user REALLY wants to delete
|
||||
# todo: show list of files before prompting to confirm
|
||||
# send warning as stderr to bypass s
|
||||
echo -e "ARE YOU CERTAIN YOU WISH TO DELETE ALL MATCHING FILES RECURSIVELY IN $search_dir?\n\tPatterns:\n\t\tWhitelist: \"${whitelist[@]}\"\n\t\tBlacklist: \"${blacklist[@]}\"\n\t\tPath: $search_dir" >&2
|
||||
read checkdelete
|
||||
|
||||
# strict case insensitive yes match, no other chars, nothing prepended or appendeed, no doubles, no mispellling
|
||||
regex="^[yY]{1}[eE]{1}[sS]{1}$"
|
||||
if [[ $checkdelete =~ $regex ]]; then
|
||||
echo "WARNING PROCEEDING TO DELETE!"
|
||||
else
|
||||
echo "NOTE: DELETE CANCELLED"
|
||||
exit 0
|
||||
fi
|
||||
|
||||
action+=' -delete'
|
||||
;;
|
||||
|
||||
# copy
|
||||
## find_ext.sh -w "txt" -c /path/somewhere
|
||||
c)
|
||||
action+=" -exec cp \"{}\" \"${OPTARG[@]}\" \\;"
|
||||
;;
|
||||
|
||||
# help
|
||||
## find_ext.sh -h
|
||||
h)
|
||||
help_usage
|
||||
exit 0
|
||||
;;
|
||||
|
||||
# invalid options or flags
|
||||
\?)
|
||||
echo "Error: Invalid Option -$OPTARG" >&2
|
||||
help_usage
|
||||
exit 0;
|
||||
;;
|
||||
|
||||
# arg requiring value not given
|
||||
## like -w without the list of exts
|
||||
:)
|
||||
echo "Error: Option -$OPTARG requires an argument" >&2
|
||||
help_usage
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
done
|
||||
echo "$whitelist_find_str"
|
||||
|
||||
# assemble the find command
|
||||
## "$PWD" used for dir instead of . to output absolute paths
|
||||
find_str="find \"$search_dir\"${whitelist_find_str}${blacklist_find_str}${action}${ending}";
|
||||
echo $find_str
|
||||
# bash -c "$find_str"
|
||||
|
||||
exit 0
|
||||
|
||||
@@ -135,9 +243,3 @@ if [ ! -z "$2" ]; then
|
||||
### appended space to make final assembly cleaner
|
||||
blacklist_find_str+='\) '
|
||||
fi
|
||||
|
||||
# assemble the find command
|
||||
## "$PWD" used for dir instead of . to output absolute paths
|
||||
find_str="find \"$PWD\"${whitelist_find_str}${blacklist_find_str}-print";
|
||||
echo "$find_str"
|
||||
bash -c "$find_str"
|
||||
Reference in new issue
Block a user