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
asked Oct 30, 2012 at 4:41
4
  • 1
    rubyquicktips.com/post/5862861056/execute-shell-commands exec('ls ~') Commented Oct 30, 2012 at 4:45
  • I think this is a duplicate question. Refer this [link][1] [1]: stackoverflow.com/questions/2232/… Commented Oct 30, 2012 at 4: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. Commented Oct 30, 2012 at 5: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? Commented Oct 30, 2012 at 5:36

4 Answers 4

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
161k44 gold badges222 silver badges308 bronze badges
answered Oct 30, 2012 at 4:48
1
  • "If you need to use the output of your command in your Ruby script use backticks", or IO.popen or Open3. Commented Oct 30, 2012 at 5: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.

answered Oct 30, 2012 at 5:32
1
  • That is a good link, I used it as a reference to make a direct answer. (I'm just letting you know) Commented Oct 30, 2012 at 7:06
1

You can try these approaches:

  1. %x[command]
  2. Kernel.system"command"
  3. run "command"
Mischa
43.4k8 gold badges101 silver badges111 bronze badges
answered Oct 30, 2012 at 5:37
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}
answered Oct 30, 2012 at 4:44
1
  • it can contain another ruby code. I mean: how can I do that in Ruby script? Commented Oct 30, 2012 at 4:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.