From 0289b7cebcfbaafacd90a782332f36122b50495d Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Fri, 14 Apr 2017 22:58:55 +0300 Subject: sums_list_paths: handle special characters --- %HOME%/.bash_utils/distr.sh | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) (limited to '%HOME%') diff --git a/%HOME%/.bash_utils/distr.sh b/%HOME%/.bash_utils/distr.sh index fe44e75..2f51a96 100644 --- a/%HOME%/.bash_utils/distr.sh +++ b/%HOME%/.bash_utils/distr.sh @@ -17,10 +17,10 @@ sums_list_paths() ( shift case "$key" in -h|--help) - echo "usage: ${FUNCNAME[0]} [-h|--help] [-0|-z]" + echo "usage: ${FUNCNAME[0]} [-h|--help] [-0|--null|-z|--zero]" return 0 ;; - -0|-z) + -0|-null|-z|--zero) fmt='%s\0' ;; *) @@ -33,15 +33,24 @@ sums_list_paths() ( [ -e "$sums_path" ] || return 0 local -a paths=() - - local path - while IFS= read -r path; do + local sum path + + while IFS= read -d ' ' -r sum; do + local escaped= + if [ "${sum#'\'}" != "$sum" ]; then + escaped=1 + sum="${sum:1}" + fi + IFS= read -r path + path="${path#'*'}" + if [ -n "$escaped" ]; then + path="${path//'\\'/$'\\'}" + path="${path//'\n'/$'\n'}" + fi paths+=("$path") - done < <( sed --binary -- 's/^\\\?[[:alnum:]]\+ [ *]//' "$sums_path" ) - - [ "${#paths[@]}" -eq 0 ] && return 0 + done < "$sums_path" - printf -- "$fmt" ${paths[@]+"${paths[@]}"} + [ "${#paths[@]}" -gt 0 ] && printf -- "$fmt" ${paths[@]+"${paths[@]}"} ) sums_update() ( -- cgit v1.2.3