OILS / spec / bin / read_from_fd.py View on Github | oilshell.org

29 lines, 17 significant
1#!/usr/bin/env python2
2from __future__ import print_function
3"""
4read_from_fd.py
5"""
6
7import os
8import sys
9
10
11def main(argv):
12 # Note: the shell has to open this fd
13 for arg in sys.argv[1:]:
14 fd = int(arg)
15 #print 'reading from', fd
16 try:
17 in_str = os.read(fd, 1024)
18 except OSError as e:
19 print('FATAL: Error reading from fd %d: %s' % (fd, e),
20 file=sys.stderr)
21 sys.exit(1)
22
23 s = '%d: ' % fd
24 sys.stdout.write(s.encode('utf-8'))
25 sys.stdout.write(in_str) # write binary data to stdout
26
27
28if __name__ == '__main__':
29 main(sys.argv)