-
Notifications
You must be signed in to change notification settings - Fork 1
/
merge-recursively
executable file
·47 lines (46 loc) · 1.03 KB
/
merge-recursively
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash
unzip=
args=
OPTIND=1
while getopts "z" OPT "$@"; do
case $OPT in
z)
unzip=1
args="-z"
;;
esac
done
shift $(($OPTIND - 1))
batch_size=8
if [ $# -eq 0 ]; then
echo "use: [ SORT_OPTIONS=... ] $0 [ -z ] <file> [ <file> ... ]" >&2
exit 1
elif [ $# -eq 1 ]; then
exec zc "$1"
elif [ ! $unzip ] && [ $# -le $batch_size ]; then
exec sort -m --batch-size=$batch_size $SORT_OPTIONS "$@"
elif [ $# -le $batch_size ]; then
command="sort -m --batch-size=$batch_size $SORT_OPTIONS"
for f in "$@"; do
command="$command <(exec zc $f)"
done
eval $command
else
let "n=$#/8"
command="sort -m --batch-size=$batch_size $SORT_OPTIONS"
if [ $n -eq 1 ]; then
if [ ! $unzip ]; then
command="$command ${@:1:7}"
else
for f in "${@:1:7}"; do
command="$command <(exec zc $f)"
done
fi
else
for i in $(seq 0 6); do
command="$command <(exec $0 $args ${@:$(($n * $i + 1)):$n})"
done
fi
command="$command <(exec $0 $args ${@:$(($n * 7 + 1))})"
eval $command
fi