3 # Ref/Attrib: [1] Tange, Ole. GNU Parallel with Bash Array. 2019-03-24. https://unix.stackexchange.com/a/508365/411854
4 # Depends: GNU Parallel, GNU Bash v5.1.16, feh 3.6.3
6 #===Declare local functions===
7 yell
() { echo "$0: $*" >&2; } # print script path and all args to stderr
8 die
() { yell
"$*"; exit 111; } # same as yell() but non-zero exit status
9 must
() { "$@" || die
"cannot $*"; } # runs args as command, reports args if command fails
11 # Desc: If arg is a command, save result in assoc array 'appRollCall'
12 # Usage: checkapp arg1 arg2 arg3 ...
14 # Input: global assoc. array 'appRollCall'
15 # Output: adds/updates key(value) to global assoc array 'appRollCall'
21 if command -v "$arg" 1>/dev
/null
2>&1; then # Check if arg is a valid command
22 appRollCall
[$arg]="true";
23 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi;
25 appRollCall
[$arg]="false"; returnState
="false";
29 #===Determine function return code===
30 if [ "$returnState" = "true" ]; then
35 } # Check that app exists
37 # Desc: If arg is a file path, save result in assoc array 'fileRollCall'
38 # Usage: checkfile arg1 arg2 arg3 ...
40 # Input: global assoc. array 'fileRollCall'
41 # Output: adds/updates key(value) to global assoc array 'fileRollCall';
42 # Output: returns 0 if app found, 1 otherwise
48 if [ -f "$arg" ]; then
49 fileRollCall
["$arg"]="true";
50 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi;
52 fileRollCall
["$arg"]="false"; returnState
="false";
56 #===Determine function return code===
57 if [ "$returnState" = "true" ]; then
62 } # Check that file exists
64 # Desc: If arg is a dir path, save result in assoc array 'dirRollCall'
65 # Usage: checkdir arg1 arg2 arg3 ...
67 # Input: global assoc. array 'dirRollCall'
68 # Output: adds/updates key(value) to global assoc array 'dirRollCall';
69 # Output: returns 0 if app found, 1 otherwise
75 if [ -d "$arg" ]; then
76 dirRollCall
["$arg"]="true";
77 if ! [ "$returnState" = "false" ]; then returnState
="true"; fi
79 dirRollCall
["$arg"]="false"; returnState
="false";
83 #===Determine function return code===
84 if [ "$returnState" = "true" ]; then
89 } # Check that dir exists
91 # Desc: Displays missing apps, files, and dirs
92 # Usage: displayMissing
94 # Input: associative arrays: appRollCall, fileRollCall, dirRollCall
95 # Output: stderr: messages indicating missing apps, file, or dirs
96 # Depends: bash 5, checkAppFileDir()
97 local missingApps value appMissing missingFiles fileMissing
98 local missingDirs dirMissing
100 #==BEGIN Display errors==
101 #===BEGIN Display Missing Apps===
102 missingApps
="Missing apps :";
103 #for key in "${!appRollCall[@]}"; do echo "DEBUG:$key => ${appRollCall[$key]}"; done
104 for key
in "${!appRollCall[@]}"; do
105 value
="${appRollCall[$key]}";
106 if [ "$value" = "false" ]; then
107 #echo "DEBUG:Missing apps: $key => $value";
108 missingApps
="$missingApps""$key ";
112 if [ "$appMissing" = "true" ]; then # Only indicate if an app is missing.
113 echo "$missingApps" 1>&2;
116 #===END Display Missing Apps===
118 #===BEGIN Display Missing Files===
119 missingFiles
="Missing files:";
120 #for key in "${!fileRollCall[@]}"; do echo "DEBUG:$key => ${fileRollCall[$key]}"; done
121 for key
in "${!fileRollCall[@]}"; do
122 value
="${fileRollCall[$key]}";
123 if [ "$value" = "false" ]; then
124 #echo "DEBUG:Missing files: $key => $value";
125 missingFiles
="$missingFiles""$key ";
129 if [ "$fileMissing" = "true" ]; then # Only indicate if an app is missing.
130 echo "$missingFiles" 1>&2;
133 #===END Display Missing Files===
135 #===BEGIN Display Missing Directories===
136 missingDirs
="Missing dirs:";
137 #for key in "${!dirRollCall[@]}"; do echo "DEBUG:$key => ${dirRollCall[$key]}"; done
138 for key
in "${!dirRollCall[@]}"; do
139 value
="${dirRollCall[$key]}";
140 if [ "$value" = "false" ]; then
141 #echo "DEBUG:Missing dirs: $key => $value";
142 missingDirs
="$missingDirs""$key ";
146 if [ "$dirMissing" = "true" ]; then # Only indicate if an dir is missing.
147 echo "$missingDirs" 1>&2;
150 #===END Display Missing Directories===
152 #==END Display errors==
153 } # Display missing apps, files, dirs
155 if ! checkapp feh parallel
; then displayMissing
; die
"FATAL:Missing apps."; fi;
157 }; # check dependencies
159 # Desc: Consumes stdin; outputs as stdout lines
160 # Input: stdin (consumes)
161 # Output: stdout (newline delimited)
162 # Example: printf "foo\nbar\n" | read_stdin
163 # Depends: GNU bash (version 5.1.16)
165 local input_stdin output
;
168 if [[ -p /dev
/stdin
]]; then
169 input_stdin
="$(cat -)";
172 # Store as output array elements
174 if [[ -n $input_stdin ]]; then
175 while read -r line
; do
177 done < <(printf "%s\n" "$input_stdin");
181 printf "%s\n" "${output[@]}";
182 }; # read stdin to stdout lines
184 # Desc: Reads arguments; outputs as stdout lines
186 # Output: stdout (newline delimited)
187 # Example: read_psarg "$@"
188 # Depends: GNU bash (version 5.1.16)
190 local input_psarg output
;
193 if [[ $# -gt 0 ]]; then
197 # Store as output array elements
198 ## Read in positional arguments
199 if [[ -n $input_psarg ]]; then
206 printf "%s\n" "${output[@]}";
207 }; # read positional argument to stdout lines
209 # Desc: print file list to stdout via `find` using script parameters
210 # Input: arg1: path to dir
212 # var: pattern_find_iregex
214 if [[ ! -d "$1" ]]; then return 1; fi;
215 must
find "$1" -maxdepth "$fdepth" -type f
-iregex "$firegex" -size +"$fsize";
216 }; # print file list to stdout from dir with script parameters
218 # Depends: read_stdin_psarg() v0.0.1, check_depends()
220 declare -a dirs_stdin dirs_psarg
;
221 declare -a paths_images
;
222 declare list_paths_images
;
225 #Populate dirs_stdin and dirs_psarg arrays
226 ## Read stdin as lines
227 re_dotfile
="^\."; # first char is a dot
228 while read -r line
; do
230 if [[ ! -d "$line" ]]; then
231 echo "ERROR:Not a dir:$line" 1>&2;
234 dir_name
="$(basename "$line")";
236 if [[ "$dir_name" =~
$re_dotfile ]]; then
237 echo "ERROR:Is a dotdir:$line" 1>&2;
240 dirs_stdin
+=("$line");
241 done < <(read_stdin
);
242 ## Read positional arguments as lines
243 re_dotfile
="^\."; # first char is a dot
244 while read -r line
; do
246 if [[ ! -d "$line" ]]; then
247 echo "ERROR:Not a dir:$line" 1>&2;
250 dir_name
="$(basename "$line")";
252 if [[ "$dir_name" =~
$re_dotfile ]]; then
253 echo "ERROR:Is a dotdir:$line" 1>&2;
256 dirs_psarg
+=("$line");
257 done < <(read_psarg
"$@");
259 # Catch all arrays empty
260 if [[ "${#dirs_stdin[@]}" -le 0 ]] && [[ "${#dirs_psarg[@]}" -le 0 ]]; then
261 die
"FATAL:No valid directories provided.";
266 firegex
=".+\(jpg\|jpeg\|gif\|png\|webm\)$"; # update according to `find . -type f | grep -Eo "\.([[:alnum:]])+$" | sort -u`
267 fsize
="10k"; # default: minimum "10k"
268 export firegex fsize
; # export for parallel
269 ## Call find_filelist() in parallel for positional argument input
270 if [[ "${#dirs_psarg[@]}" -gt 0 ]]; then
271 fdepth
=10; export fdepth
; # 10 for dirs from positional arguments
272 paths_images
+=("$( parallel find_flist {} "$fdepth" "$firegex" "$fsize" ::: "${dirs_psarg[@]}" )"); # See [1]
274 ## Call find_filelist() in parallel for stdin input
275 if [[ "${#dirs_stdin[@]}" -gt 0 ]]; then
276 fdepth
=1; export fdepth
; # 1 ofr dirs from stdin
277 paths_images
+=("$( parallel find_flist {} "$fdepth" "$firegex" "$fsize" ::: "${dirs_stdin[@]}" )"); # See [1]
280 # Convert paths_images array into file list
281 for i
in "${!paths_images[@]}"; do
282 list_paths_images
="$(printf "%s
\n%s
" "${paths_images[$i]}" "$list_paths_images")";
285 file_count
="$(wc -l < <(echo -n "$list_paths_images"))";
286 echo "$DEBUG:file_count:$file_count"
289 # Sort, remove duplicate paths
290 list_paths_images
="$(echo "$list_paths_images" | sort -u | tr -s '\n')";
292 # Remove paths with dotfiles
293 list_paths_images
="$(echo "$list_paths_images" | grep -viE "/\.
" )";
296 list_paths_images_tmp
="/dev/shm/$(date +%Y%m%dT%H%M%S.%N%z)"..feh_paths.txt
;
297 must
echo -n "$list_paths_images" > "$list_paths_images_tmp";
300 yell
"STATUS:Built file list in $SECONDS seconds.";
302 # Run feh with filelist
303 feh
--full-screen --auto-zoom --draw-filename --filelist "$list_paths_images_tmp";
306 must
rm "$list_paths_images_tmp";
308 export -f yell die must read_stdin read_psarg find_flist
;
309 #==END Define local functions==
313 # Author: Steven Baltakatei Sandoval