Forum: Ruby A question about running external program

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Zhao Y. (Guest)
on 2009-01-07 10:30
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 "|"?
Brian C. (Guest)
on 2009-01-07 12:28
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"
Justin C. (Guest)
on 2009-01-07 12:43
(Received via mailing list)
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
Albert S. (Guest)
on 2009-01-07 14:01
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/ ]
Brian C. (Guest)
on 2009-01-07 15:03
Justin C. 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"
This topic is locked and can not be replied to.