How to return value of shell script to rails?

2019-08-24 11:37发布

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 ?

2条回答
2楼-- · 2019-08-24 12:00

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
查看更多
一夜七次
3楼-- · 2019-08-24 12:01

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"

查看更多
登录 后发表回答