Eric Thiele
Eric Thiele

Reputation: 19

Ruby shell execution using %x not working

I'm using uname -n as an example. I've tried other shell commands, using the full pathname to the shell command, and I used other delimiters, such as %x( ) and %x[ ].

$ uname -n
my-server
$ which env
/usr/bin/env
$ which ruby
/home/ubuntu/.rvm/rubies/ruby-2.2.1/bin/ruby
$ irb
2.2.1 :001 > %x{uname -n}
 => "my-server\n" 
2.2.1 :002 > exit
$ cat ET.rb

    #!/usr/bin/env ruby

    %x{uname -n}

$ ruby ET.rb
$ ### !!!?!?!?  I'm expecting "my-server"

Upvotes: 0

Views: 187

Answers (1)

pangpang
pangpang

Reputation: 8831

In IRB, it will show the results of any Ruby statements you feed it. But if in a Ruby script, you should use puts or print method to print something out:

puts %x{uname -n}

or:

print %x{uname -n}

Upvotes: 1

Related Questions