0

I have written a shell script which executing some of the mysql commands.

I want to run that shell script from rails side. And want to get result back of commands to rails from shell script.

How may i handle it?

EDIT:

script.sh

mysql -u root -pmysql << eof
SELECT TABLE_NAME AS "Table Name", table_rows AS "Quant of Rows", ROUND((data_length + index_length)/1024/1024,2) AS "Total Size Mb"  FROM information_schema.TABLES WHERE information_schema.TABLES.table_schema='database_name';
eof

This is my script. How may i return SELECT query result to rails ?

3
  • Hi, answer of Fareesh Vijayarangam is correct, you can use backticks, but since your "script" is in fact an sql query, I would suggest you running this query inside Rails, with ActiveRecord. Keep scripts when you need to link to command-line programs. Commented Mar 22, 2011 at 6:50
  • @plang How may i run this query from inside Rails ? Commented Mar 22, 2011 at 6:52
  • This is pure rails stuff: look at Active Record in your favorite documentation. Active Record is the Object Relational Mapper of Rails. Commented Mar 22, 2011 at 6:57

2 Answers 2

2

You can do something like this, it will block until the script is finished and any output that it sent to standard out will be contained in the output variable.

output = `/my/script.sh`
puts output
Sign up to request clarification or add additional context in comments.

2 Comments

I have updated my question. Can you tell me how may i pass SELECT query value to standard output ?
Since you are inside rails already, use ActiveRecord::Base.connection.execute(sql_query) to run it directly, without the script.
2

Use backticks ` `

`/path/to/script` #backticks

Edit

To get the output of a mysql statement, use the -e flag

Eg. mysql -e "SELECT * from information_schema"

3 Comments

I have updated my question. Can you tell me how may i pass SELECT query value to standard output ?
I have pasted my script into root directory of my Rails Project.while trying to run shell script from console.>> `show_table_size.sh` (irb):17: command not found: show_table_size.sh
You need to use ./file-name-of-script. Also ensure that the interpreter directive (hashbang) is set at the first line of the shell script, eg. #!/bin/sh

Your Answer

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.