add proper error handling for all final exec calls
[hband-tools.git] / user-tools / pipeby
blob09a22f945b66bcc5ee731a9578940c2d409c80e0
1 #!/bin/bash
3 true <<'EOF'
4 =pod
6 =head1 NAME
8 pipeby - Run a command and pipe its output to an other one
10 =head1 SYNOPSIS
12 pipeby 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 pipeby(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 pipeby(1) commands together to get a pipeline equivalent to
26 C<CMD_1 | CMD_2 | CMD_3>, like:
28 pipeby CMD_1 -- pipeby 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[@]}"