1
|
#!/bin/bash
|
2
|
# Expands bash-style {} expressions.
|
3
|
# See <http://www.gnu.org/software/bash/manual/bash.html#Brace-Expansion>.
|
4
|
# Filters each line from stdin to stdout.
|
5
|
# Warning: Due to shell limitations, a blank line will be interpreted as EOF.
|
6
|
|
7
|
sedEreFlag="$(test "$(uname)" = Darwin && echo E || echo r)"
|
8
|
|
9
|
sed () { "$(which sed)" -"$sedEreFlag" "$@";}
|
10
|
|
11
|
echoEach () { local line; for line in "$@"; do echo "$line"; done; }
|
12
|
|
13
|
singleQuote="'"
|
14
|
singleQuoteEsc="'\''"
|
15
|
CR=$'\r'
|
16
|
LF=$'\n'
|
17
|
|
18
|
while true; do
|
19
|
read # get next line from stdin; strips trailing newlines
|
20
|
line="$REPLY"
|
21
|
test -z "$line" && break # EOF if no line or empty line
|
22
|
|
23
|
line="${line//$CR/}" # remove \r
|
24
|
line="${line//$singleQuote/$singleQuoteEsc}" # escape single quotes
|
25
|
# Escape non-brace chars; don't expand ./{
|
26
|
line="$(echo "$line"\
|
27
|
|sed -e "s/[^{,}]+/'&'/g"|sed -e "s/\.\/'\{'([^}]*)'\}'/.\/{\1}/g")"
|
28
|
|
29
|
eval echoEach $line # brace-expand $line
|
30
|
done
|