Pergunta

I would like to run a shell script from my ruby file. Then capture its output and analyze it.

This is the scenario: - Within my ruby script I need to execute the my_script.sh - The shell script produce the followinf output in the terminal

> my_script.sh
  xxxxx 1111
  yyyyy 2222
  zzzzz 3333
  • I need to capture and analyse this ouput inside the ruby script in order to find if a keyword is displayed (e.g., yyyy).

I'm using the following comand:

my_script = "/home/script.sh"
system("sh #{my_script}")

I'm not able to capture the output produced in the terminal for parsing

Foi útil?

Solução

Don't use system, it does not capture STDOUT. Use backticks (or %x()):

output = %x( #{my_script} )
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top