EXTREMELY crude example of using anonymous pipes to communicate between a bash and python script running in parallel (NOTE: Only tested in default OS X Mavericks shell)
To run it yourself: ./app1.sh | python app2.py
| #!/bin/bash - | |
| set -o nounset # Treat unset variables as an error | |
| # doing some stuff and simulating processing time by sleeping | |
| echo 'Initial setup' | |
| sleep 2 | |
| # now we trigger python script | |
| echo 'APP2_GO' | |
| # pretending to do other stuff | |
| echo 'super cool processing is happening right now!' | |
| sleep 2 | |
| # now we tell python script we're done | |
| echo 'APP2_DONE' |
| #!/usr/bin/python | |
| import sys | |
| import time | |
| k = 0 | |
| try: | |
| buff = '' | |
| while True: | |
| buff += sys.stdin.read(1) | |
| if buff.endswith('\n'): | |
| # output line of text received from app1.sh | |
| print "app1 msg", (k, buff[:-1]) | |
| # handle text triggers below | |
| if buff[:-1] == "APP2_GO": | |
| print "Python app starts doing radical stuff now!" | |
| elif buff[:-1] == "APP2_DONE": | |
| print "Python app now knows the shell script is done!" | |
| break | |
| buff = '' | |
| k = k + 1 | |
| except KeyboardInterrupt: | |
| sys.stdout.flush() | |
| pass |