run linux commands as ruby scripts

N

nkb

Hi.
I've 2 commands to run on my Linux.
A "ls -l" command, followed by a tar command. I would like to have the
commands in separate lines.

I tried this

exec "ls -l"
exec "tar tvf myfilename"

Only the "ls -l" is executed. I'm not sure what is happening. Is it
possible to execute a series of commands in Ruby? Thanks for any help.
 
B

Bill Atkins

Well, exec will execute the command passed to it and _replace_ the
current process with the command you just ran. So when you make that
call, the Ruby process gets replaced with "ls -l" which then finishes.

Use system "command" instead and the command will be executed and then
control will return to your script.

Bill
 
I

I did not

Hi.
I've 2 commands to run on my Linux.
A "ls -l" command, followed by a tar command. I would like to have the
commands in separate lines.

I tried this

exec "ls -l"
exec "tar tvf myfilename"

Only the "ls -l" is executed. I'm not sure what is happening. Is it
possible to execute a series of commands in Ruby? Thanks for any help.

Use back quotes:

a=`ls -l`
b=`tar tvf yourfilename`

`cmd` returns STDOUT from the cmd.
`cmd 2>&1` is you want STDERR (mixed with STDOUT)

Look into popen and popen3 if you need to interact with cmd

Hth.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top