diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2017-04-18 06:30:41 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2017-04-18 06:30:41 +0300 |
commit | eb95ca2796a8722212176c5210151e6a8d981566 (patch) | |
tree | d896d90f04e2a8157b39e259e28e823cc6c2f421 | |
parent | distr.sh: bugfix (diff) | |
download | linux-home-eb95ca2796a8722212176c5210151e6a8d981566.tar.gz linux-home-eb95ca2796a8722212176c5210151e6a8d981566.zip |
add sums_remove_missing
Diffstat (limited to '')
-rw-r--r-- | %HOME%/.bash_utils/distr.sh | 108 |
1 files changed, 92 insertions, 16 deletions
diff --git a/%HOME%/.bash_utils/distr.sh b/%HOME%/.bash_utils/distr.sh index 4b55107..e0fa710 100644 --- a/%HOME%/.bash_utils/distr.sh +++ b/%HOME%/.bash_utils/distr.sh @@ -25,18 +25,26 @@ _sums_unescape_path() ( sums_list_paths() ( set -o errexit -o nounset -o pipefail - local fmt='%s\n' + local print_lines= + local print_sums= + local zero_terminated= while [ "$#" -gt 0 ]; do local key="$1" shift case "$key" in -h|--help) - echo "usage: ${FUNCNAME[0]} [-h|--help] [-0|--null|-z|--zero]" + echo "usage: ${FUNCNAME[0]} [-h|--help] [-0|--null|-z|--zero] [-l|--lines] [-s|--sums]" return 0 ;; -0|-null|-z|--zero) - fmt='%s\0' + zero_terminated=1 + ;; + -l|--lines) + print_lines=1 + ;; + -s|--sums) + print_sums=1 ;; *) echo "${FUNCNAME[0]}: unrecognized parameter: $key" >&2 @@ -45,24 +53,41 @@ sums_list_paths() ( esac done + local fmt_line='%s\n' + [ -n "$zero_terminated" ] && fmt_line='%s\0' + + local fmt_output="$fmt_line" + + [ -n "$print_lines" ] && fmt_output="$fmt_line$fmt_output" + [ -n "$print_sums" ] && fmt_output="$fmt_output$fmt_line" + [ -e "$sums_path" ] || return 0 - local -a paths=() - local sum path + local -a output=() + local line - 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#'*'}" - [ -n "$escaped" ] && path="$( _sums_unescape_path "$path" )" - paths+=("$path") + while IFS= read -r line; do + { + local path sum + + IFS= read -d ' ' -r sum + local escaped= + if [ "${sum#'\'}" != "$sum" ]; then + escaped=1 + sum="${sum:1}" + fi + + IFS= read -r path + path="${path#'*'}" + [ -n "$escaped" ] && path="$( _sums_unescape_path "$path" )" + + [ -n "$print_lines" ] && output+=("$line") + output+=("$path") + [ -n "$print_sums" ] && output+=("$sum") + } <<< "$line" done < "$sums_path" - [ "${#paths[@]}" -gt 0 ] && printf -- "$fmt" ${paths[@]+"${paths[@]}"} + [ "${#output[@]}" -gt 0 ] && printf -- "$fmt_output" ${output[@]+"${output[@]}"} ) sums_add() ( @@ -123,3 +148,54 @@ sums_verify() ( sha1sum --check --strict --quiet -- "$sums_path" ) + +sums_remove_missing() ( + set -o errexit -o nounset -o pipefail + + local dry_run= + + while [ "$#" -gt 0 ]; do + local key="$1" + shift + case "$key" in + -h|--help) + echo "usage: ${FUNCNAME[0]} [-h|--help] [-n|--dry-run]" + return 0 + ;; + -n|--dry-run) + dry_run=1 + ;; + *) + echo "${FUNCNAME[0]}: unrecognized parameter: $key" >&2 + return 1 + ;; + esac + done + + local -a input=() + local -a paths=() + local line path + + while IFS= read -d '' -r line; do + IFS= read -d '' -r path + input+=("$line") + paths+=("$path") + done < <( sums_list_paths -z --lines ) + + local -a output=() + + local i + for i in "${!paths[@]}"; do + if [ ! -e "${paths[$i]}" ]; then + echo "${FUNCNAME[0]}: doesn't exist: ${paths[$i]}" + else + output+=("${input[$i]}") + fi + done + + [ -n "$dry_run" ] && return 0 + + for line in ${output[@]+"${output[@]}"}; do + echo "$line" + done > "$sums_path" +) |