How to return value of shell script to rails?

2019-08-24 11:15发布

问题:

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 ?

回答1:

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


回答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"