143 lines
5.0 KiB
Bash
143 lines
5.0 KiB
Bash
#!/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
|
|
case "$opt" in
|
|
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
|
|
|
|
### 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
|
|
### only add -o after the first one
|
|
if [ $whitelist_counter -eq 0 ]; then
|
|
#### test if array has exactly one item
|
|
if [ ${#whitelist[@]} -eq 1 ]; then
|
|
##### Append space because its the only entry
|
|
whitelist_find_str+=" -iname \"*.${ext}\" "
|
|
else
|
|
##### no appended space on first one to make it neat with the -o's
|
|
whitelist_find_str+=" -iname \"*.${ext}\""
|
|
fi
|
|
else
|
|
whitelist_find_str+=" -o -iname \"*.${ext}\" "
|
|
fi
|
|
|
|
### increment counter
|
|
whitelist_counter=$(($whitelist_counter + 1));
|
|
done
|
|
## finish the whitelist group
|
|
### appended space to make final assembly cleaner
|
|
whitelist_find_str+='\) '
|
|
;;
|
|
esac
|
|
done
|
|
echo "$whitelist_find_str"
|
|
|
|
exit 0
|
|
|
|
if [ -z "$1" ]; then
|
|
echo -e 'Usage:\n\tfind_ext.sh <str extensions whitelist> <str extensions blacklist>\n\tfind_ext.sh "jpg mp3 png" ["doc pdf svg"]'
|
|
exit 1
|
|
else
|
|
# break whitelist str into array
|
|
IFS=' ' read -r -a whitelist <<< "$1"
|
|
|
|
# create the whitelisted find section text
|
|
## 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
|
|
### only add -o after the first one
|
|
if [ $whitelist_counter -eq 0 ]; then
|
|
#### test if array has exactly one item
|
|
if [ ${#whitelist[@]} -eq 1 ]; then
|
|
##### Append space because its the only entry
|
|
whitelist_find_str+=" -iname \"*.${ext}\" "
|
|
else
|
|
##### no appended space on first one to make it neat with the -o's
|
|
whitelist_find_str+=" -iname \"*.${ext}\""
|
|
fi
|
|
else
|
|
whitelist_find_str+=" -o -iname \"*.${ext}\" "
|
|
fi
|
|
|
|
### increment counter
|
|
whitelist_counter=$(($whitelist_counter + 1));
|
|
done
|
|
## finish the whitelist group
|
|
### appended space to make final assembly cleaner
|
|
whitelist_find_str+='\) '
|
|
fi
|
|
|
|
if [ ! -z "$2" ]; then
|
|
# break blacklist str into array
|
|
IFS=' ' read -r -a blacklist <<< "$2"
|
|
|
|
# create the blacklist find section text
|
|
## start the whitelist group
|
|
## has the -not for blacklisting
|
|
## prepended space to make final assembly cleaner
|
|
blacklist_find_str=' -not \('
|
|
## 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
|
|
### only add -o after the first one
|
|
if [ $blacklist_counter -eq 0 ]; then
|
|
#### 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
|
|
else
|
|
blaclklist_find_str+=" -o -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+='\) '
|
|
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" |