A question about running external program


#1

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 “|”?


#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 “|”?

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”


#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 “|”?

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


#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/ ]


#5

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”