0

I would like to open a terminal from my ruby script, display the script outputs and exit the terminal at the end of the script.

# open xterm -> `mate-terminal -e " ... "`
a = ''
while a != 'q'

  a = STDIN.getch

  puts "Hello World !" # output in xterm
end

# quit xterm

Is this possible or do I have to do a script to call another one?

EDIT:

I can do with two scripts :

test1.rb

#!/usr/bin/env ruby
`mate-terminal -e "bash -c 'ruby test2.rb'"`

test2.rb

#!/usr/bin/env ruby
puts "hello world"
gets

run first script

$ ruby test1.rb

But is it possible to do this with a single script?

EDIT2 :
I try to do that

#!/usr/bin/env ruby
obj1 = "object 1"
obj2 = "object 2"

file = <<EOF
#!/usr/bin/env ruby
require 'io/console'
a = ''
while a != 'q'
    puts "Do you want to see obj1, obj2 or quit ? .. enter 1, 2 or q"
    a = STDIN.getch
    case a
    when '1' then puts "#{obj1}"
    when '2' then puts "#{obj2}"
    end
end
EOF

File.open('display.rb', 'w') { |f| f.puts file }

`mate-terminal -e "bash -c 'ruby display.rb'"`

It works, but it's really not beautifull.
Is there another way?

µgeek
  • 43
  • 5

1 Answers1

0

Do you want to fork your current process? Not sure what you mean by => "display the script outputs". Which script?

You'll want to read this post. Calling shell commands from Ruby

You might want to check IO.popen. You'll note, in the code below, that we first pipe the string whoami to the shell and then pass the string whoami to puts.

#! /usr/bin/env ruby

if __FILE__ == $0

  f = IO.popen("bash", "w")
  f.puts "whoami"
  f.close

  puts "whoami"

end

output

gerard
whoami

Is this what you are looking for?

G4143
  • 2,624
  • 4
  • 18
  • 26
  • Have you explored IO.popen? https://www.rubydoc.info/stdlib/core/IO.popen – G4143 Oct 05 '19 at 16:27
  • I don't think that's exactly what I'm looking for. I want to run a script in the background ( like a daemon ) to display specific information through a terminal ( like a popup ). – µgeek Oct 05 '19 at 17:11
  • This is very important. Do you want to pass a one time message or one command to the terminal or do you want to have a connection between the background process(daemon) and the terminal(shell) that you can continue to update? – G4143 Oct 05 '19 at 17:34
  • Yes, I want a connection between the background and the terminal. I want to be able to choose the information displayed. – µgeek Oct 05 '19 at 17:47
  • The thing is, I think you want a connection to the shell. If you need to display the shell results to the user, then you should use a GUI toolkit like Tk. I might be wrong but I don't think you can form that connection: process => terminal => shell... I should clarify, I can't think of a way to do it. – G4143 Oct 05 '19 at 18:00
  • I want to do ruby_process => terminal => ruby_process instead. But maybe it's above my level. I have to learn more first. Thanks for your help. – µgeek Oct 05 '19 at 18:28
  • I don't understand why the terminal(a terminal is just a GUI wrapper around a shell(bash in some cases)) is in that equation. Why not have a ruby_process which spawns another ruby_process? Why do you need that middle step? – G4143 Oct 05 '19 at 18:42