1

I have a Rails webapp full of students with test scores. Each student has exactly one test score.

I want the following functionality:

1.) The user enters an arbitrary test score into the website and presses "enter"

2.) "Some weird magic where data is passed from Rails database to bash script"

3.) The following bash script is run:

./tool INPUT file.txt

where:

INPUT = the arbitrary test score

file.txt = a list of all student test scores in the database

4.) "More weird magic where output from the bash script is sent back up to a rails view and made displayable on the webpage"

And that's it.

I have no idea how to do the weird magic parts.

My attempt at a solution:

In the rails dbconsole, I can do this:

SELECT score FROM students;

which gives me a list of all the test scores (which satisfies the "file.txt" argument to the bash script).

But I still don't know how my bash script is supposed to gain access to that data.

Is my controller supposed to pass the data down to the bash script? Or is my model supposed to? And what's the syntax for doing so?

I know I can run a bash script from the controller like this:

system("./tool")

But, unfortunately, I still need to pass the arguments to my script, and I don't see how I can do that...

ineedahero
  • 488
  • 2
  • 7
  • 22
  • Use [`Open3`](http://ruby-doc.org/stdlib-2.3.1/libdoc/open3/rdoc/Open3.html) from the standard library. – mu is too short Oct 14 '16 at 18:49
  • for simple cases you can just use string interpolation to pass arguments. See [Best practices with STDIN in Ruby?](http://stackoverflow.com/questions/273262/best-practices-with-stdin-in-ruby) – max pleaner Oct 14 '16 at 18:59
  • Even then, how do I return the output of the bash script to my rails webapp? – ineedahero Oct 14 '16 at 19:18

1 Answers1

0

You can just use the built-in ruby tools for running shell commands:

https://ruby-doc.org/core-2.3.1/Kernel.html#method-i-60

For example, in one of my systems I need to get image orientation:

exif_orientation = `exiftool -Orientation -S "#{image_path}"`.to_s.chomp

Judging from my use of .to_s, running the command may sometimes return nil, and I don't want an error trying to chomp nil. A normal output includes the line ending which I feed to chomp.

Jim
  • 475
  • 2
  • 7