File: reorder-o.sh

package info (click to toggle)
postgresql-pllua 1%3A2.0.10-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,316 kB
  • sloc: ansic: 14,369; sql: 2,181; makefile: 163; sh: 59; javascript: 38
file content (44 lines) | stat: -rwxr-xr-x 768 bytes parent folder | download | duplicates (4)
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
#!/bin/sh

cmd="$1"
shift 1

findpos() {
    while :; do
	[ "$#" -lt 2 ] && { echo "no -o option found" >&2; return 1; }
	[ "x$1" = "x-o" ] && { echo "$#"; return 0; }
	shift 1;
    done
}

# cmd x x x -o y z z z

nx=$(findpos "$@")
[ -z "$nx" ] && exit 1

n_before=$(expr "$#" - "$nx")
n_after=$(expr "$nx" - 2)
outfile=$(shift $n_before; printf '%s:' "$2")
outfile="${outfile%?}"

while :; do
    if [ "$n_before" -gt 0 ]
    then
	set -- "$@" "$1";
	shift 1;
	n_before=$(expr "$n_before" - 1);
    elif [ "$n_before" -eq 0 ]
    then
	shift 2;
	n_before="-1";
    elif [ "$n_after" -gt 0 ]
    then
	set -- "$@" "$1"
	shift 1
	n_after=$(expr "$n_after" - 1)
    else
	exec "$cmd" "$@" "$outfile"
	exit 127
    fi
done
echo "something went badly wrong" >&2; exit 1;