Created
May 6, 2021 05:42
-
-
Save wyatt8740/ebe9416e39f8811ce1884dc51c471a90 to your computer and use it in GitHub Desktop.
mcomix image shuffler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env bash | |
# EXAMPLE USAGE: to shuffle all files in a directory and not traverse recursively or shuffle in subdirectories, try | |
# mcomix-shuf -f -d 1 . | |
# -f: only files, no directories | |
# -d (number): depth to traverse | |
# to make this more posix-y, don't use 'shuf -z'. Also, it might work without bash. | |
# you also will need to stop using readlink for true POSIX-ness. | |
2>/dev/null unset DEPTH | |
2>/dev/null unset FILEONLY | |
2>/dev/null unset PARSING | |
# alternative POSIX-compatible 'shuf -z' implementation if 'shuf' is not | |
# present (shuf is a non-standard utility included in GNU Coreutils) | |
# Note that this shuf output might be the same if executed multiple times | |
# per second. It also doesn't handle newlines in filenames, but if you have | |
# those you should know better than to run any shell script you did not write | |
# yourself with that in mind because you're asking for trouble. | |
if [ -z "$(command -v shuf)" ]; then | |
shuf() { | |
if [ "$1" = '-z' ]; then | |
shift | |
fi | |
tr '\0' '\n' "$@"| | |
awk 'BEGIN {srand(); OFMT="%.17f"} {print rand(), $0}' | | |
sort -k1,1n | cut -d ' ' -f2- | tr '\n' '\0' | |
} | |
else | |
if [ -z "$(command -v which)" ]; then | |
SHUFPROG="`which shuf`" | |
else | |
SHUFPROG='/usr/bin/shuf' | |
fi | |
shuf() | |
{ | |
"$SHUFPROG" --random-source=/dev/urandom "$@" | |
} | |
fi | |
DEPTH='' | |
FILEONLY='' | |
PARSING=0 | |
if [ "$#" -gt 0 ]; then | |
while test $PARSING -eq 0; do | |
case "$1" in | |
'-d') | |
if [ "$#" -gt 1 ]; then | |
shift | |
DEPTH="$1" | |
shift | |
else | |
1>&2 echo "Error: no depth specified, but '-d' was given. '-d' requires a maximum depth" | |
1>&2 echo "for traversal to be given." | |
exit 1 | |
fi | |
;; | |
'-f' | '--files-only') | |
FILEONLY='-type f' | |
shift | |
;; | |
'-h' | '--help') | |
echo 'Usage:' | |
echo "$0"' [flags/params] PATH(S)' | |
echo "Flags:" | |
echo " -d [number]: Maximum depth to traverse. Default: infinite." | |
echo " -f, --files-only: Files only. Do not pass directory names to mcomix." | |
echo " -h, --help: Show this help and exit." | |
exit 0 | |
;; | |
*) | |
# stop parsing args | |
PARSING=1 | |
;; | |
esac | |
done | |
fi | |
if [ "$#" -eq 0 ]; then | |
WORKDIRS="$PWD" | |
else | |
WORKDIRS=$@ | |
fi | |
#echo $WORKDIRS | |
# if depth is not empty then we must have set it | |
if test -z $DEPTH; then | |
# need -L to find links too | |
# find -L $WORKDIRS $FILEONLY -print0 | shuf -z | xargs -0 mcomix | |
# to stop it from passing directories instead of files (for symlinked dirs): | |
find -L $WORKDIRS $FILEONLY -print0 | shuf -z | tr '\0' '\n' |while IFS= read -r line; do if [[ -f "$(readlink -f "$line")" ]]; then echo "$line"; fi; done | tr '\n' '\0' | xargs -0 mcomix | |
else | |
# find -L $WORKDIRS -maxdepth "$DEPTH" $FILEONLY -print0 | shuf -z | xargs -0 mcomix | |
# to stop it from passing directories instead of files (for symlinked dirs): | |
find -L $WORKDIRS -maxdepth "$DEPTH" $FILEONLY -print0 | shuf -z | tr '\0' '\n' |while IFS= read -r line; do if [[ -f "$(readlink -f "$line")" ]]; then echo "$line"; fi; done | tr '\n' '\0' | xargs -0 mcomix | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment