std/c/README.md: Add section skeletons and don't indent lists
[sunny256-utils.git] / line_exec.py
blob95df573eb6c42da7d276d2fb9bdde877dd522c51
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
4 """line_exec.py - Send all lines from stdin to specified programs
6 Reads from stdin and sends every line to the program(s) specified on the
7 command line.
9 File ID: 0a869b50-f45a-11e2-bb76-a088b4ddef28
10 License: GNU General Public License version 2 or later.
12 """
14 import sys
15 import subprocess
18 def exec_line(line, args):
19 for cmd in args:
20 pipe = subprocess.Popen(
21 cmd,
22 stdin=subprocess.PIPE,
23 stdout=subprocess.PIPE,
24 stderr=subprocess.PIPE,
26 (stdout, stderr) = pipe.communicate(line)
27 sys.stdout.write(stdout)
28 sys.stdout.flush()
29 sys.stderr.write(stderr)
30 sys.stderr.flush()
33 def main(args=None):
34 from optparse import OptionParser
36 parser = OptionParser()
37 parser.add_option(
38 "-A",
39 "--after",
40 type="string",
41 dest="after",
42 default="",
43 help="Text string to add after every line output",
44 metavar="TEXT",
46 parser.add_option(
47 "-B",
48 "--before",
49 type="string",
50 dest="before",
51 default="",
52 help="Text string to add before every line output",
53 metavar="TEXT",
55 (opt, args) = parser.parse_args()
57 fp = sys.__stdin__
58 line = fp.readline()
59 while line:
60 sys.stdout.write(opt.before)
61 exec_line(line, args)
62 sys.stdout.write(opt.after)
63 line = fp.readline()
66 if __name__ == "__main__":
67 main()