From 966a8d1189ee6e224fcc4cbee330c3e6df7fb077 Mon Sep 17 00:00:00 2001 From: Steven Baltakatei Sandoval Date: Tue, 17 Jan 2023 00:25:46 +0000 Subject: [PATCH] feat(user/bkfeh):Ignore dotfile directories (e.g. `./.Photos`) - feat(unitproc/bkt-filter_no_dots):Add bash function to filter out stdin lines with dotfile basenames. (Note: Quite slow). - chore(unitproc/bkt-read_stdin_psarg):Remove debug code and fix how posarg is read (error found via shellcheck). --- unitproc/bkt-filter_no_dots | 56 +++++++++++++++++++++++++++++++++++ unitproc/bkt-read_stdin_psarg | 7 ++--- user/bkfeh | 26 +++++++++++----- 3 files changed, 77 insertions(+), 12 deletions(-) create mode 100755 unitproc/bkt-filter_no_dots mode change 100644 => 100755 user/bkfeh diff --git a/unitproc/bkt-filter_no_dots b/unitproc/bkt-filter_no_dots new file mode 100755 index 0000000..3846bc3 --- /dev/null +++ b/unitproc/bkt-filter_no_dots @@ -0,0 +1,56 @@ +#!/usr/bin/env bash + +filter_no_dots() { + # Desc: Filters stdin lines for paths with basenames that are not dotfiles + # Usage: printf "foo\n" | filter_no_dots [path] + # Input: stdin (consumes) + # args + # Output: stdout (only outputs lines if basename is not a dotfile) + # Version: 0.0.1 + # Depends: GNU bash (v5.1.16) + + local input_stdin input_psarg buffer re output; + + # Store stdin + if [[ -p /dev/stdin ]]; then + input_stdin="$(cat -)"; + fi; + + # Store arguments + if [[ $# -gt 0 ]]; then + input_psarg="$*"; + fi; + + # Combine as buffer array elements + ## Read in stdin + if [[ -n $input_stdin ]]; then + while read -r line; do + buffer+=("$line"); + done < <(printf "%s\n" "$input_stdin"); + fi; + ## Read in positional arguments + if [[ -n $input_psarg ]]; then + for arg in "$@"; do + buffer+=("$arg"); + done; + fi; + + # Form output array + re="^\."; # first char is a dot + while read -r line; do + # Get path basename + file_name="$(basename "$line")"; + # Add path to output if basename not a dotfile + if [[ ! "$file_name" =~ $re ]]; then + output+=("$line"); + else + continue; + fi; + done < <( printf "%s\n" "${buffer[@]}" ); + + # Print output array to stdout + printf "%s\n" "${output[@]}"; +}; # Filters stdin lines for paths with basenames that are not dotfiles + +# Test +printf "foo\n.bar\nbaz\n" | filter_no_dots "$HOME/.local" "$HOME/" diff --git a/unitproc/bkt-read_stdin_psarg b/unitproc/bkt-read_stdin_psarg index d29e89a..aad2927 100644 --- a/unitproc/bkt-read_stdin_psarg +++ b/unitproc/bkt-read_stdin_psarg @@ -11,20 +11,18 @@ read_stdin_psarg() { # Output: stdout (newline delimited) # Example: read_stdin_psarg "$@" # Depends: GNU bash (version 5.1.16) - # Version: 0.0.1 + # Version: 0.0.3 local input_stdin input_psarg output; # Store stdin if [[ -p /dev/stdin ]]; then input_stdin="$(cat -)"; fi; - yell "DEBUG:$(declare -p input_stdin)"; # Store arguments if [[ $# -gt 0 ]]; then - input_psarg="$@"; + input_psarg="$*"; fi; - yell "DEBUG:$(declare -p input_psarg)"; # Combine as output array elements ## Read in stdin @@ -39,7 +37,6 @@ read_stdin_psarg() { output+=("$arg"); done; fi; - yell "DEBUG:$(declare -p output)"; # Print to stdout printf "%s\n" "${output[@]}"; diff --git a/user/bkfeh b/user/bkfeh old mode 100644 new mode 100755 index bdc543a..d2f5423 --- a/user/bkfeh +++ b/user/bkfeh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# Version: 0.0.3 +# Version: 0.0.5 # Ref/Attrib: [1] Tange, Ole. GNU Parallel with Bash Array. 2019-03-24. https://unix.stackexchange.com/a/508365/411854 # Depends: GNU Parallel, GNU Bash v5.1.16, feh 3.6.3 @@ -162,19 +162,19 @@ read_stdin_psarg() { # Output: stdout (newline delimited) # Example: read_stdin_psarg "$@" # Depends: GNU bash (version 5.1.16) - # Version: 0.0.1 + # Version: 0.0.3 local input_stdin input_psarg output; # Store stdin if [[ -p /dev/stdin ]]; then input_stdin="$(cat -)"; fi; - + # Store arguments if [[ $# -gt 0 ]]; then - input_psarg="$@"; + input_psarg="$*"; fi; - + # Combine as output array elements ## Read in stdin if [[ -n $input_stdin ]]; then @@ -203,6 +203,7 @@ print_filelist() { }; # print file list to stdout from dir with script parameters main() { # Depends: read_stdin_psarg() v0.0.1, check_depends() + local re_dotfile; declare -a main_dirs; declare -a paths_images; declare list_paths_images; @@ -217,13 +218,24 @@ main() { #Populate main_dirs array ## Read stdin and positional arguments as lines + re_dotfile="^\."; # first char is a dot while read -r line; do + # Check if dir if [[ ! -d "$line" ]]; then echo "ERROR:Not a dir:$line" 1>&2; continue; fi; + dir_name="$(basename "$line")"; + # Exclude dotdirs + if [[ "$dir_name" =~ $re_dotfile ]]; then + echo "ERROR:Is a dotdir:$line" 1>&2; + continue + fi; main_dirs+=("$line"); done < <(read_stdin_psarg "$@"); + + # Catch empty main_dirs array + if [[ "${#main_dirs[@]}" -le 0 ]]; then die "FATAL:No valid directories provided."; fi; # Generate file list paths_images+=("$( parallel print_filelist {} ::: "${main_dirs[@]}" )"); # See [1] @@ -236,8 +248,8 @@ main() { file_count="$(wc -l < <(echo -n "$list_paths_images"))"; echo "$DEBUG:file_count:$file_count" done; - - # Sort and remove duplicates + + # Sort, remove duplicate paths list_paths_images="$(echo "$list_paths_images" | sort -u | tr -s '\n')"; # Write -- 2.30.2