WIP: uniproc
[hband-tools.git] / user-tools / ffilt
blobfc06b2c035949241dfe7be26d818ae9208bd4fe8
1 #!/bin/bash
3 true <<'EOF'
4 =pod
6 =head1 NAME
8 ffilt - Filter a file via a command's STDIO and write back to it
10 =head1 SYNOPSIS
12 ffilt I<FILE> I<COMMAND> [I<ARGS>]
14 =head1 DESCRIPTION
16 Feed I<FILE> into I<COMMAND>'s stdin, then save its stdout back to I<FILE>
17 if I<COMMAND> ran successfully.
19 Similar to this shell script:
21 output=`cat I<FILE> | I<COMMAND>`
22 [ $? = 0 ] && echo "$output" > I<FILE>
24 =head1 LIMITATIONS
26 =head1 SEE ALSO
28 sponge(1)
30 =cut
32 EOF
35 set -e
36 set -o pipefail
37 set -u
39 . /usr/lib/tool/bash-utils
42 opt_verbose=no
45 while [ $# != 0 ]
47 case "$1" in
48 --help)
49 pod2text "$0"
50 exit 0;;
51 -v|--verbose)
52 opt_verbose=yes
54 --)
55 shift
56 break;;
57 -*)
58 errx -1 "unknown option: $1";;
60 break;;
61 esac
62 shift
63 done
66 file=$1
67 shift
69 stdout=`cat "$file" | command "$@"`
70 # pipefail let us die here if either cat or command failed
71 cat > "$file" <<< "$stdout"