469,622 Members | 2,103 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,622 developers. It's quick & easy.

here document as script and read from stdin

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
3 2627
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
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
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.

Similar topics

3 posts views Thread by Nader Emami | last post: by
reply views Thread by Joe Burnett | last post: by
3 posts views Thread by eight02645999 | last post: by
3 posts views Thread by ifmusic | last post: by
2 posts views Thread by Brendan Fay | last post: by
1 post views Thread by monomaniac21 | last post: by
4 posts views Thread by Adam Funk | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.