By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,530 Members | 1,570 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,530 IT Pros & Developers. It's quick & easy.

here document as script and read from stdin

P: n/a
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.
--
Robin Becker
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Robin Becker wrote:
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?
--
Robin Becker
Jul 18 '05 #2

P: n/a
In article <40************@jessikat.fsnet.co.uk>,
Robin Becker <ro***@SPAMREMOVEjessikat.fsnet.co.uk> wrote:
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, do**@u.washington.edu
Jul 18 '05 #3

P: n/a
Robin Becker <ro***@SPAMREMOVEjessikat.fsnet.co.uk> writes:
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.
--
Robin Becker

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
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.