make getpeername() return the original socket address which before it was intercepted
[hband-tools.git] / user-tools / pipecmd
blob1fb31c9c83825125c7a7819811e5f2d6bc7a7db7
1 #!/bin/bash
3 true <<'EOF'
4 =pod
6 =head1 NAME
8 pipecmd - Run a command and pipe its output to an other one
10 =head1 SYNOPSIS
12 pipecmd I<CMD_1> [I<ARGS>] -- I<CMD_2> [I<ARGS>]
14 =head1 DESCRIPTION
16 Equivalent to this shell command:
18 CMD_1 | CMD_2
20 The first command's (I<CMD_1>) arguments can not contain a double-dash (C<-->),
21 because it's the command separator for pipecmd(1).
22 However, since only a total of 2 commands are supported,
23 arguments for I<CMD_2> may contain double-dash(es).
25 You can chain pipecmd(1) commands together to get a pipeline equivalent to
26 C<CMD_1 | CMD_2 | CMD_3>, like:
28 pipecmd CMD_1 -- pipecmd CMD_2 -- CMD_3
30 =head1 RATIONALE
32 It's sometimes more convenient to don't involve shell command-line parser.
34 =head1 SEE ALSO
36 pipexec(1)
38 =cut
40 EOF
43 set -e
44 set -o pipefail
45 set -u
47 if [ $# = 0 ]
48 then
49 pod2text "$0" >&2
50 exit 1
52 if [ "$1" = --help ]
53 then
54 pod2text "$0"
55 exit 0
58 cmd_n=1
59 declare -a cmd_1=()
60 declare -a cmd_2=()
62 while [ $# != 0 ]
64 if [ ".$1" = '.--' -a $cmd_n == 1 ]
65 then
66 cmd_n=2
67 else
68 eval "cmd_${cmd_n}+=(\"\$1\")"
70 shift
71 done
73 "${cmd_1[@]}" | "${cmd_2[@]}"