here document as script and read from stdin

Discussion in 'Python' started by Robin Becker, Apr 20, 2004.

  1. Robin Becker

    Robin Becker Guest

    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
     
    Robin Becker, Apr 20, 2004
    #1
    1. Advertising

  2. Robin Becker

    Robin Becker Guest

    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
     
    Robin Becker, Apr 20, 2004
    #2
    1. Advertising

  3. Robin Becker

    Donn Cave Guest

    In article <>,
    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.


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

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

    Donn Cave,
     
    Donn Cave, Apr 20, 2004
    #3
  4. Robin Becker

    Eddie Corns Guest

    Robin Becker <> 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
     
    Eddie Corns, Apr 21, 2004
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Johnathan Doe

    peek at stdin, flush stdin

    Johnathan Doe, May 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    25,457
    Chatoyer
    May 17, 2013
  2. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    823
    Dan Pop
    Jun 21, 2004
  3. Ben
    Replies:
    2
    Views:
    1,404
    jacob navia
    Aug 29, 2009
  4. Terry Cooper
    Replies:
    7
    Views:
    451
    Janos Sebok
    Jun 9, 2009
  5. Stefano Sabatini
    Replies:
    6
    Views:
    313
    Stefano Sabatini
    Jul 29, 2007
Loading...

Share This Page