A question about running external program

Discussion in 'Ruby' started by Zhao Yi, Jan 7, 2009.

  1. Zhao Yi

    Zhao Yi Guest

    I use this method to run an external program:

    output=`uname -r | sed 's/\([^\.]*\.[^\.]*\)\.*.*/\1/'`
    puts output

    ruby will ignore the sed command. Doesn't ruby support "|"?
    --
    Posted via http://www.ruby-forum.com/.
     
    Zhao Yi, Jan 7, 2009
    #1
    1. Advertising

  2. Zhao Yi wrote:
    > I use this method to run an external program:
    >
    > output=`uname -r | sed 's/\([^\.]*\.[^\.]*\)\.*.*/\1/'`
    > puts output
    >
    > ruby will ignore the sed command. Doesn't ruby support "|"?
    >


    The sed command is not being ignored. The issue is that the backticks
    (`) do string interpolation before sending the command, which means your
    backslashes are removed before the commands are sent to the shell.

    Take a look at this:

    irb(main):001:0> puts `uname -r | sed 's/\([^\.]*\.[^\.]*\)\.*.*/\1/' &&
    ps x | grep uname`
    2.6.27.5-desktop-2mnb
    24178 pts/8 S+ 0:00 sh -c uname -r | sed 's/([^.]*.[^.]*).*.*/?/'
    && ps x | grep uname
    24182 pts/8 S+ 0:00 grep uname
    => nil
    irb(main):002:0> puts system 'uname -r | sed
    "s/\([^\.]*\.[^\.]*\)\.*.*/\1/" && ps x | grep uname'
    2.6
    24187 pts/8 S+ 0:00 sh -c uname -r | sed
    "s/\([^\.]*\.[^\.]*\)\.*.*/\1/" && ps x | grep uname
    24191 pts/8 S+ 0:00 grep uname
    true
    => nil
    irb(main):003:0> puts `uname -r | sed
    's/\\([^\\.]*\\.[^\\.]*\\)\\.*.*/\\1/' && ps x | grep uname`
    2.6
    24201 pts/8 S+ 0:00 sh -c uname -r | sed
    's/\([^\.]*\.[^\.]*\)\.*.*/\1/' && ps x | grep uname
    24205 pts/8 S+ 0:00 grep uname
    => nil

    -Justin
     
    Justin Collins, Jan 7, 2009
    #2
    1. Advertising

  3. Zhao Yi wrote:
    > I use this method to run an external program:
    >
    > output=`uname -r | sed 's/\([^\.]*\.[^\.]*\)\.*.*/\1/'`
    > puts output
    >
    > ruby will ignore the sed command. Doesn't ruby support "|"?


    That's not a smart question.

    - Show what happens if you run the same script at the command line
    - Show what 'output' actually contains when doing this in Ruby
    - Show what version of Ruby you are using, and under what platform

    FWIW, it works just fine for me under Ubuntu Hardy with ruby-1.8.6
    compiled from source:

    $ ruby -v
    ruby 1.8.6 (2007-09-24 patchlevel 111) [i486-linux]
    $ irb
    irb(main):001:0> output=`uname -r | sed 's/\([^\.]*\.[^\.]*\)\.*.*/\1/'`
    => "2.6.24-22-386\n"
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Jan 7, 2009
    #3
  4. Zhao Yi wrote:
    > I use this method to run an external program:
    >
    > output=`uname -r | sed 's/\([^\.]*\.[^\.]*\)\.*.*/\1/'`
    > puts output
    >
    > ruby will ignore the sed command. Doesn't ruby support "|"?


    Instead of strugling with shell-escaping, you can do the regexp part in
    Ruby itself:

    output = `uname -r`
    version = output[ /some-regexp/ ]
    --
    Posted via http://www.ruby-forum.com/.
     
    Albert Schlef, Jan 7, 2009
    #4
  5. Justin Collins wrote:
    > irb(main):003:0> puts `uname -r | sed
    > 's/\\([^\\.]*\\.[^\\.]*\\)\\.*.*/\\1/' && ps x | grep uname`


    I think that's not quite right, because \\1 is still converted into a
    single character with ASCII code 1, rather than being passed to sed as
    \1

    irb(main):013:0> `echo 's/\\([^\\.]*\\.[^\\.]*\\)\\.*.*/\\1/'`
    => "s/\\([^\\.]*\\.[^\\.]*\\)\\.*.*/\001/\n"

    So as far as I can see, you need to use \\\\1 to get a literal \
    followed by 1

    irb(main):014:0> `echo 's/\\([^\\.]*\\.[^\\.]*\\)\\.*.*/\\\\1/'`
    => "s/\\([^\\.]*\\.[^\\.]*\\)\\.*.*/\\1/\n"

    Escapes like \. and \( are OK, but for consistency you can escape them
    all in the same way:

    irb(main):015:0> `echo
    's/\\\\([^\\\\.]*\\\\.[^\\\\.]*\\\\)\\\\.*.*/\\\\1/'`
    => "s/\\([^\\.]*\\.[^\\.]*\\)\\.*.*/\\1/\n"
    --
    Posted via http://www.ruby-forum.com/.
     
    Brian Candler, Jan 7, 2009
    #5
    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. Replies:
    2
    Views:
    462
  2. Rigga
    Replies:
    13
    Views:
    550
    Rigga
    Mar 3, 2005
  3. Replies:
    0
    Views:
    291
  4. Replies:
    1
    Views:
    247
    Dennis Lee Bieber
    Nov 25, 2006
  5. Heikki Toivonen
    Replies:
    2
    Views:
    276
    Heikki Toivonen
    Mar 30, 2007
Loading...

Share This Page