1

Let's say I have some terminal commands like:

sudo mycommand1
mycommand2
#.....

What should I do run them via ruby script (not bash) in Ubuntu?

UPDATE: I have a ruby script:

def my_method1()
  #calculating something.....
end

def method2(var1, var2)
  #how do I sudo mycommand1 and any other Lunix command from here?
end

def method3(var4)
  #calculating something2....
end
Alexandre
  • 13,030
  • 35
  • 114
  • 173
  • 1
    http://rubyquicktips.com/post/5862861056/execute-shell-commands `exec('ls ~')` – XMen Oct 30 '12 at 04:45
  • I think this is a duplicate question. Refer this [link][1] [1]: http://stackoverflow.com/questions/2232/calling-bash-commands-from-ruby – Nehal J Wani Oct 30 '12 at 04:47
  • 1
    My question is, why do you want to run them in Ruby vs. the shell? Many simple scripts run faster and can be done very easily by wiring several commands together with pipes. The equivalent commands in Ruby, Perl or Python are more convoluted and run more slowly. I enjoy programming in Ruby, but sometimes it's not the right tool for the job. – the Tin Man Oct 30 '12 at 05:18
  • @AlexMaslakov do you need to call either `my_method`, `method2`, or `method3` from the command line - and then those commands call other shell commands? – New Alexandria Oct 30 '12 at 05:36

4 Answers4

1

You can do system, exec, or place the command in backticks.

exec("mycommand") will replace the current process so that's really only pratical at the end of your ruby script.

system("mycommand") will create a new process and return true if the command succeeded and nil otherwise.

If you need to use the output of your command in your Ruby script use backticks:

response = 'mycommand`
the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Anthony DeSimone
  • 1,994
  • 1
  • 12
  • 21
  • "If you need to use the output of your command in your Ruby script use backticks", or [`IO.popen`](http://www.ruby-doc.org/core-1.9.3/IO.html#method-c-popen) or [`Open3`](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/open3/rdoc/index.html). – the Tin Man Oct 30 '12 at 05:25
1

There are many questions on SO that answer this. However you can run a command in many ways using system, exec, (backticks), %x{} or using open3. I prefer to use open3 -

require 'open3'

log = File.new("#{your_log_dir}/script.log", "w+")
command = "ls -altr ${HOME}"

Open3.popen3(command) do |stdin, stdout, stderr|
    log.puts "[OUTPUT]:\n#{stdout.read}\n"
    unless (err = stderr.read).empty? then 
        log.puts "[ERROR]:\n#{err}\n"
    end
end

If you want to know more about other options you can refer to Ruby, Difference between exec, system and %x() or Backticks for links to relevant documentation.

Community
  • 1
  • 1
saihgala
  • 5,724
  • 3
  • 34
  • 31
  • That is a good link, I used it as a reference to make a direct answer. (I'm just letting you know) – yeyo Oct 30 '12 at 07:06
1

You can try these approaches:

  1. %x[command]
  2. Kernel.system"command"
  3. run "command"
Mischa
  • 42,876
  • 8
  • 99
  • 111
Heena Hussain
  • 3,673
  • 1
  • 20
  • 21
0

make some file.rb with:

#!/path/to/ruby

system %{sudo mycommand1}
system %{mycommand2}

and the chmod the file with exec permissions (e.g. 755)

It you need to pass variables between the two commands, run them together:

system %{sudo mycommand1; \
         mycommand2}
New Alexandria
  • 6,951
  • 4
  • 57
  • 77