Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby only executing the first line?

Tags:

shell

ruby

exec

I'm writing a ruby script and found this strange behaviour.

Using ruby 2.4.2 [x86_64-darwin16]

Basically I'm trying to echo two separated messages and in my index.rb file I got:

exec("echo 'teste'")
exec("echo 'teste2'")

But when I run ruby ./index.rb

The output is:

teste

Why that's happening?

Shouldn't this be the output?

testeteste2
like image 426
Pedro Sturmer Avatar asked Mar 04 '23 12:03

Pedro Sturmer


1 Answers

exec([env,] command... [,options])

Replaces the current process by running the given external command docs

It means that the first call to exec replaces your ruby program with echo, so the rest of the ruby program is not executed.

You can use backticks to run a command like you want:

`echo 'teste'`
`echo 'teste2'`
like image 100
mrzasa Avatar answered Mar 08 '23 23:03

mrzasa