Project

General

Profile

1 3772 aaronmk
#!/bin/bash
2 3771 aaronmk
# 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 3772 aaronmk
singleQuote="'"
14
singleQuoteEsc="'\''"
15
CR=$'\r'
16
LF=$'\n'
17 3771 aaronmk
18
while true; do
19 3772 aaronmk
    read # get next line from stdin; strips trailing newlines
20
    line="$REPLY"
21 3771 aaronmk
    test -z "$line" && break # EOF if no line or empty line
22 3772 aaronmk
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 3771 aaronmk
done