-
-
Save oguz-ismail/28bf3150a111f8ad36a207f763729cba to your computer and use it in GitHub Desktop.
Code generation - Brace expansion
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
#!/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