Skip to content

Instantly share code, notes, and snippets.

@oguz-ismail
Last active February 22, 2021 06:26
Show Gist options
  • Save oguz-ismail/28bf3150a111f8ad36a207f763729cba to your computer and use it in GitHub Desktop.
Save oguz-ismail/28bf3150a111f8ad36a207f763729cba to your computer and use it in GitHub Desktop.
Code generation - Brace expansion
#!/bin/bash -
gen_sfx()
case $ext in
(/*|*//*|*/)
printf '%q: %q: Invalid specifier\n' "$0" "$ext" >&2
exit 3 ;;
(*/*)
local - IFS=/
set -f
set -- $ext
set -- "${@@Q}"
IFS=,
sfx={$*} ;;
(*)
sfx='"$ext"'
esac
case $# in
(0|1)
printf 'Usage: %q DIR EXT[/EXT...] CMD [ARG...]\n' "$0" >&2
exit 1 ;;
(2)
set -- "$@" printf '%s\n' ;&
(*)
dir=$1 ext=$2
shift 2
esac
if ! [[ -d $dir ]]; then
printf '%q: %q: Not a directory\n' "$0" "$dir" >&2
exit 2
fi
gen_sfx
shopt -s globstar nullglob
eval 'set -- "$@" "$dir"/**/*.'"$sfx"
unset -f gen_sfx
unset dir ext sfx
shopt -u globstar nullglob
"$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment