Skip to content

Instantly share code, notes, and snippets.

@fetsorn
Last active May 14, 2022 22:35
Show Gist options
  • Save fetsorn/82263a79af4db904c8bfec7a6eefee3f to your computer and use it in GitHub Desktop.
Save fetsorn/82263a79af4db904c8bfec7a6eefee3f to your computer and use it in GitHub Desktop.
A tree cat that executes.
#!/bin/bash
# "a tree cat that executes"
# suggested by @that-other-guy
# at https://stackoverflow.com/questions/70841809/what-should-this-bash-script-be-called
unset concatenate
OPTIND=1
while getopts 'c' opt; do
case $opt in
c) concatenate=1 ;;
,*) printf "%s\n" "OPTIONS: -c Concatenate files in INPUTPATH before piping to 'eval COMMAND'" >&2
exit 1
esac
done
shift "$(( OPTIND - 1 ))"
if [ $# -ne 4 ]; then
printf 1>&2 "%s\n" "Usage: $0 [-c] INPUTPATH COMMAND OUTPUTDIR OUTPUTNAME
Pipes files in INPUTPATH to 'eval COMMAND'
writes output to OUTPUTDIR/files/DATE-OUTPUTNAME
creates symbolic link at OUTPUTDIR/links/OUTPUTNAME
OPTIONS:
-c Concatenate files in INPUTPATH before piping to 'eval COMMAND'
EXAMPLE:
$0 \"assets/pictures\" \"scripts/process-assets.sh\" \"output\" \"pictures.txt\""
exit 3
fi
inputpath="$1"
cmd="$2"
outputdir="$3"
outputname="$4"
filedir="$outputdir/files"
linkdir="$outputdir/links"
mkdir -p "$filedir" "$linkdir"
starttime=$(date +%Y%m%d-%H%M%S)
outputfile="$filedir/$starttime-$outputname"
outputlink="$linkdir/$outputname"
if [[ "$concatenate" -gt 0 ]]; then
input=$(find "$inputpath" ! -name ".DS_Store" | sort | xargs cat)
else
input=$(find "$inputpath" ! -name ".DS_Store" | sort)
fi
amount=$(wc -l <<< "$input");
printf 1>&2 "| %s | %s | %s | %s\n" "$inputpath" "$outputdir" "$outputname" "$amount";
eval "$cmd" <<< "$input" > "$outputfile"
rm -f "$outputlink"
ln -s "$(realpath "$outputfile")" "$outputlink"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment