From: Steven Baltakatei Sandoval Date: Mon, 30 Jan 2023 17:40:06 +0000 (+0000) Subject: feat(user/bkfeh):Save up to 100MB or 100 sample images if envvar set X-Git-Tag: 0.8.1~17 X-Git-Url: https://zdv2.bktei.com/gitweb/BK-2020-03.git/commitdiff_plain/4db78240f391329fcfea3310423e719815edf6cd?ds=sidebyside;hp=-c feat(user/bkfeh):Save up to 100MB or 100 sample images if envvar set - Note: Environment variable is BKFEH_SAMPLE_DIR in which contents will be deleted and sample images saved upon every run of bkfeh. --- 4db78240f391329fcfea3310423e719815edf6cd diff --git a/user/bkfeh b/user/bkfeh index 7dbf9fb..c0410ef 100755 --- a/user/bkfeh +++ b/user/bkfeh @@ -1,5 +1,5 @@ #!/usr/bin/env bash -# Version: 0.0.7 +# Version: 0.1.0 # 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 @@ -214,6 +214,50 @@ find_flist() { if [[ ! -d "$1" ]]; then return 1; fi; must find "$1" -maxdepth "$fdepth" -type f -iregex "$firegex" -size +"$fsize"; }; # print file list to stdout from dir with script parameters +save_sample() { + # Usage: save_sample arg1 + # Input: arg1 list_paths (list of files to take samples from) + # envvar BKFEH_SAMPLE_DIR (environment variable set outside of this script) + # Depends: yell(), GNU Parallel, GNU find, GNU Coreutils 8.32 (cut, find, du) + local list_paths + sample_count="100"; + sample_max_space="100000000"; # bytes + + if [[ ! -v BKFEH_SAMPLE_DIR ]]; then return 0; fi; # return early if environment var not set. + + if [[ ! -z "$1" ]]; then + list_paths="$1"; # newline-delimited list of file paths to sample from + else + yell "ERROR:NO paths available to sample."; + fi; + + if [[ -d "$BKFEH_SAMPLE_DIR" ]]; then + sample_dir="$BKFEH_SAMPLE_DIR"; + yell "STATUS:Environment variable BKFEH_SAMPLE_DIR set. Clearing and saving samples..."; + + ## clear previous sample + count_samples="$(find $BKFEH_SAMPLE_DIR -maxdepth 1 -type f | wc -l)"; + find "$BKFEH_SAMPLE_DIR" -maxdepth 1 -type f -exec rm '{}' \; ; + + ## save random sample + yell "STATUS:Saving random sample of size $sample_count to $BKFEH_SAMPLE_DIR..."; + list_paths_sample="$(echo "$list_paths" | shuf | head -n"$sample_count")"; + while read -r line; do + if [[ -z "$line" ]]; then continue; fi; + ### check size limit + sample_act_space="$(du -bd1 "$BKFEH_SAMPLE_DIR" | cut -f1 )"; # actual used space + cand_space="$(du -bd1 "$line" | cut -f1 )"; # size of candidate file to add + sample_req_space="$((sample_act_space + cand_space))"; + if [[ "$sample_req_space" -lt "$sample_max_space" ]]; then + #### add file to sample dir + cp -n "$line" "$BKFEH_SAMPLE_DIR" ; + fi; + done < <( echo "$list_paths_sample" ); + else + yell "ERROR:Does not exist: $BKFEH_SAMPLE_DIR"; + fi; +}; # save sample of files + main() { # Depends: read_stdin_psarg() v0.0.1, check_depends() local re_dotfile; @@ -226,6 +270,7 @@ main() { ## Read stdin as lines re_dotfile="^\."; # first char is a dot while read -r line; do + line="$(readlink -e "$line")"; # Check if dir if [[ ! -d "$line" ]]; then echo "ERROR:Not a dir:$line" 1>&2; @@ -242,6 +287,7 @@ main() { ## Read positional arguments as lines re_dotfile="^\."; # first char is a dot while read -r line; do + line="$(readlink -e "$line")"; # Check if dir if [[ ! -d "$line" ]]; then echo "ERROR:Not a dir:$line" 1>&2; @@ -295,6 +341,8 @@ main() { # Write list_paths_images_tmp="/dev/shm/$(date +%Y%m%dT%H%M%S.%N%z)"..feh_paths.txt; must echo -n "$list_paths_images" > "$list_paths_images_tmp"; + ## Save sample to path in env. var. BKFEH_SAMPLE_DIR if set + save_sample "$list_paths_images"; # Print stats yell "STATUS:Built file list in $SECONDS seconds.";