Handle names with multiple subsequent whitespaces correctly

This commit is contained in:
Martchus 2017-01-12 21:40:26 +01:00
parent 310641ec25
commit 13c23b9898
1 changed files with 15 additions and 21 deletions

View File

@ -1,27 +1,33 @@
#!/bin/bash #!/bin/bash
shopt -s nullglob # from doc: If set, Bash allows filename patterns which match no files to expand to a null string, rather than themselves. # set shell options
set -e # abort on first error
shopt -s nullglob # allow filename patterns which match no files to expand to a null string, rather than themselves
shopt -s dotglob shopt -s dotglob
# define queue # define queue
queue=() queue=()
function queueecho { function queueecho {
for item in "${queue[@]}"; do for item in "${queue[@]}"; do
echo $item echo "$item"
done done
} }
function queueexec { function queueexec {
queueecho | parallel "-j$parallelcount" --eta "eval {}" queueecho | parallel "-j$parallelcount" --eta "eval {}"
} }
function iteratedirs { function iteratedirs {
for item in "$1"/*; do local dir="$1"
local depth="$2"
local current_rel_dir="$3"
for item in "$dir"/*; do
if [[ -d $item ]]; then if [[ -d $item ]]; then
if [[ $dept == none ]] || [[ $currentlevel -lt $dept ]]; then if [[ $dept == none ]] || [[ $currentlevel -lt $dept ]]; then
iteratedirs "${item}" $(($2 + 1)) "${3}${item##*/}/" iteratedirs "${item}" $(($depth + 1)) "${current_rel_dir}${item##*/}/"
fi fi
elif [[ -f $item ]]; then elif [[ -f $item ]]; then
if [[ $filter == "" ]] || [[ "$item" =~ $filter ]]; then if [[ ! $filter ]] || [[ $item =~ $filter ]]; then
name=${item##*/} name=${item##*/}
namewithoutextension=${name%.*} namewithoutextension=${name%.*}
queue+=("ITERATOR_FULL_PATH=\"$item\" ITERATOR_FILE_NAME=\"$name\" ITERATOR_FILE_NAME_WITHOUT_EXTENSION=\"$namewithoutextension\" ITERATOR_CURRENT_DIR=\"$1\" ITERATOR_CURRENT_REL_DIR=\"$3\" ITERATOR_BASE_DIR=\"$basedir\" ITERATOR_TARGET_DIR=\"$targetdir/$3\" \"$cmd\" $append") queue+=("ITERATOR_FULL_PATH=\"$item\" ITERATOR_FILE_NAME=\"$name\" ITERATOR_FILE_NAME_WITHOUT_EXTENSION=\"$namewithoutextension\" ITERATOR_CURRENT_DIR=\"$dir\" ITERATOR_CURRENT_REL_DIR=\"$current_rel_dir\" ITERATOR_BASE_DIR=\"$basedir\" ITERATOR_TARGET_DIR=\"$targetdir/$current_rel_dir\" \"$cmd\" $append")
else else
echo "${bold}${blue}Info:${normal} ${bold}Skipping »$item«.${normal}" echo "${bold}${blue}Info:${normal} ${bold}Skipping »$item«.${normal}"
fi fi
@ -38,17 +44,7 @@ blue=$(tput setaf 4)
bold=$(tput bold) bold=$(tput bold)
normal=$(tput sgr0) normal=$(tput sgr0)
# parse arguments # parse arguments
read= read= argcount=0 append= basedir=. targetdir= dept=none cmd= filter= parallelcount=+0 noconfirm=
# read arguments
argcount=0
append=
basedir=./
dept=none
cmd=
filter=
targetdir=
parallelcount=+0
noconfirm=false
for arg in "$@" for arg in "$@"
do do
if [[ "--base-dir" == $arg ]]; then if [[ "--base-dir" == $arg ]]; then
@ -117,9 +113,7 @@ ITERATOR_TARGET_DIR Path of the target directory for the curre
fi fi
done done
# validate specified arguments, use base directory as target directory if not specified # validate specified arguments, use base directory as target directory if not specified
if [[ ! $targetdir ]]; then [[ $targetdir ]] || targetdir=$basedir
targetdir=$basedir
fi
if [[ ! $cmd ]]; then if [[ ! $cmd ]]; then
echo "${bold}${red}Error:${normal} ${bold}No command specified.${normal}" echo "${bold}${red}Error:${normal} ${bold}No command specified.${normal}"
exit 1 exit 1
@ -129,7 +123,7 @@ iteratedirs "$basedir" 0
if [[ ${#queue[@]} -ge 1 ]]; then if [[ ${#queue[@]} -ge 1 ]]; then
echo "${bold}Generated queue${normal}" echo "${bold}Generated queue${normal}"
queueecho queueecho
if [[ $noconfirm == true ]]; then if [[ $noconfirm ]]; then
queueexec queueexec
else else
while true; do while true; do