Professional Web Applications Themes

controlling external process' stdin/stdout AND getting its return value - Ruby

Hi all, I want total control over an external process, i.e. handle its stdin/stdout as with IO.popen (also, if it is possible, handle its stderr, too), and I also need to get is return value. How can I do this? Thanks, Ferenc...

  1. #1

    Default controlling external process' stdin/stdout AND getting its return value

    Hi all,

    I want total control over an external process, i.e. handle its
    stdin/stdout as with IO.popen (also, if it is possible, handle its
    stderr, too), and I also need to get is return value.

    How can I do this?

    Thanks,
    Ferenc



    Ferenc Guest

  2. #2

    Default Re: controlling external process' stdin/stdout AND getting its returnvalue

    On Tue, 17 Feb 2004, Ferenc Engard wrote:
     

    it's tricky to manage stdout, stderr, _and_ exit status since with popen you
    lose the sense of $?.

    try my session module from the raa:

    http://raa.ruby-lang.org/list.rhtml?name=session

    example:

    ~ > cat sess.rb

    #!/usr/bin/env ruby

    # http://raa.ruby-lang.org/list.rhtml?name=session
    require 'session'

    sh = Session::Shell.new

    # ====
    puts "EXAMPLE 0"
    stdout, stderr = sh.execute 'ls'
    puts "stdout <#{ stdout.inspect }>"
    puts "stderr <#{ stdout.inspect }>"
    puts "status <#{ sh.status }>"
    puts

    # ====
    puts "EXAMPLE 1"
    stdout, stderr = '', ''
    sh.execute 'ls does-not-exist', :stdout => stdout, :stderr => stderr
    puts "stdout <#{ stdout.inspect }>"
    puts "stderr <#{ stdout.inspect }>"
    puts "status <#{ sh.exitstatus }>"
    puts

    # ====
    puts "EXAMPLE 2"
    dev_null = open '/dev/null', 'w'
    sh.execute 'ls', :o => STDOUT, :e => dev_null
    puts "status <#{ sh.exit_status }>"
    puts

    # ====
    puts "EXAMPLE 4"
    org = sh.path
    p sh.path
    sh.path = ['.'] + sh.path
    p sh.path
    sh.path = "/bin/foo:/bin/bar:#{ sh.path.join ':' }"
    p sh.path
    sh.path = org
    p sh.path
    puts

    # ====
    puts "EXAMPLE 5"
    sh = Session::Shell.new :history => true
    sh.execute 'ls'
    sh.execute 'echo foobar'
    p sh.history[0]
    p sh.history[1]
    puts

    # ====
    puts "EXAMPLE 6"
    cmd = 'while test 1; do echo on_out; echo on_err 1>&2; sleep 1; done'
    sh.execute(cmd) do |o, e|
    puts "processing stdout <#{ o.inspect }>..." if o
    puts "processing stderr <#{ e.inspect }>..." if e
    end
    puts


    ~ > ruby sess.rb

    EXAMPLE 0
    stdout <"sess.rb\nsess.rb.out\n">
    stderr <"sess.rb\nsess.rb.out\n">
    status <0>

    EXAMPLE 1
    stdout <"">
    stderr <"">
    status <1>

    EXAMPLE 2
    sess.rb
    sess.rb.out
    status <0>

    EXAMPLE 4
    ["/data/bin", "/data/bin", "/usr/kerberos/bin", "/usr/local/bin", "/bin", "/usr/bin", "/usr/X11R6/bin", "/usr/local/java/bin", ".", "~/bin", "/sbin", "/usr/sbin", "/usr/local/sbin", "/home/ahoward/bin"]
    [".", "/data/bin", "/data/bin", "/usr/kerberos/bin", "/usr/local/bin", "/bin", "/usr/bin", "/usr/X11R6/bin", "/usr/local/java/bin", ".", "~/bin", "/sbin", "/usr/sbin", "/usr/local/sbin", "/home/ahoward/bin"]
    ["/bin/foo", "/bin/bar", ".", "/data/bin", "/data/bin", "/usr/kerberos/bin", "/usr/local/bin", "/bin", "/usr/bin", "/usr/X11R6/bin", "/usr/local/java/bin", ".", "~/bin", "/sbin", "/usr/sbin", "/usr/local/sbin", "/home/ahoward/bin"]
    ["/data/bin", "/data/bin", "/usr/kerberos/bin", "/usr/local/bin", "/bin", "/usr/bin", "/usr/X11R6/bin", "/usr/local/java/bin", ".", "~/bin", "/sbin", "/usr/sbin", "/usr/local/sbin", "/home/ahoward/bin"]

    EXAMPLE 5
    cmd: <ls>
    cmdno: <3>
    out: <"sess.rb\nsess.rb.out\n">
    err: <"">
    cid: <1729_3_142581>

    cmd: <echo foobar>
    cmdno: <4>
    out: <"foobar\n">
    err: <"">
    cid: <1729_4_69019>


    EXAMPLE 6
    processing stdout <"on_out\n">...
    processing stderr <"on_err\n">...
    processing stdout <"on_out\n">...
    processing stderr <"on_err\n">...

    (loops forever)


    contact me offline with any questions - the current docs are scant but the
    above should get you going.

    -a
    --
    ================================================== =============================
    | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
    | PHONE :: 303.497.6469
    | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
    | URL :: http://www.ngdc.noaa.gov/stp/
    | TRY :: for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done
    ================================================== =============================

    Ara.T.Howard Guest

  3. #3

    Default Re: controlling external process' stdin/stdout AND getting its return value

    Hi,

    At Tue, 17 Feb 2004 06:14:57 +0900,
    Ara.T.Howard wrote in [ruby-talk:92997]: 

    $? will be set when the pipe is closed.

    --
    Nobu Nakada


    nobu.nokada@softhome.net Guest

Similar Threads

  1. Replies: 2
    Last Post: July 13th, 04:08 PM
  2. #39215 [NEW]: Inappropriate close of stdin/stdout/stderr
    By tstarling at wikimedia dot org in forum PHP Bugs
    Replies: 9
    Last Post: November 3rd, 01:34 PM
  3. get external process's output *and* return value
    By Andrew in forum PERL Beginners
    Replies: 9
    Last Post: February 26th, 09:02 PM
  4. STDIN and STDOUT on Windows
    By Peter Kay in forum PERL Beginners
    Replies: 0
    Last Post: January 14th, 04:45 PM
  5. stdout redirected to a process's own stdin
    By Jason in forum UNIX Programming
    Replies: 4
    Last Post: September 4th, 03:24 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139