[Tutor] Shell scripting

Alan Gauld alan.gauld at btinternet.com
Wed Sep 17 21:07:12 CEST 2008


"Patrick" <optomatic at rogers.com> wrote

>I was just wondering if there was a way to return the results of a
> python script to the bash shell? 


> Here is a silly pseudo code example:
> 
> bash command | some-python-script.py |  some.other-script.sh

That's called pipelining and uses standard input/output streams.

########## Untested! : echo.py #####

import sys

for line in sys.stdin:
    sys.stdout.write(line)

#########################

$ ls | python echo.py | wc -l

Should give much the same result as

$ ls | wc -l

HTH,


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list