here document as script and read from stdin

R

Robin Becker

Is it possible to use python with a shell HERE document as the script
and read the standard input? In other words make python usable as a
filter with a side input?

It doesn't seem to be obvous unless I can somehow use the -c option.
 
R

Robin Becker

Robin said:
Is it possible to use python with a shell HERE document as the script
and read the standard input? In other words make python usable as a
filter with a side input?

It doesn't seem to be obvous unless I can somehow use the -c option.
Well I find I can put the script in as the -c option and then stdin is
still available. That requires me to be pretty careful with quotes
though. I wonder if there's an easy way to get a here document to act as
a parameter?
 
D

Donn Cave

Robin Becker said:
Is it possible to use python with a shell HERE document as the script
and read the standard input? In other words make python usable as a
filter with a side input?

It doesn't seem to be obvous unless I can somehow use the -c option.

Use "-" as the name of the file - e.g.,

python - << 'EOT'
import sys
print sys.argv
EOT

Donn Cave, (e-mail address removed)
 
E

Eddie Corns

Robin Becker said:
Is it possible to use python with a shell HERE document as the script
and read the standard input? In other words make python usable as a
filter with a side input?
It doesn't seem to be obvous unless I can somehow use the -c option.


python -c "`cat <<OOK
from sys import stdin
for line in stdin:
print line.upper(),
print 'ha ha', "ho ho"
OOK
`"

ie the value of -c can be a complete program as long as you keep the newlines
intact and don't mess up the quotes. However it would probably be a lot
simpler to copy the script to a temp file and execute that (ignoring
pathological security issues).

(Only tested with bash!)

Eddie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top