# Run linux commands as ruby scripts

**URL:** <https://rubytalk.org/t/run-linux-commands-as-ruby-scripts/14453>\
**Category:** ruby-talk\
**Created:** [28 October 2004 12:55 UTC](https://rubytalk.org/t/run-linux-commands-as-ruby-scripts/14453 "2004-10-28T12:55:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nkb](https://avatars.discourse-cdn.com/v4/letter/n/f19dbf/32.png) [@nkb](https://rubytalk.org/u/nkb)\
**Post date:** [28 October 2004 12:55 UTC](https://rubytalk.org/t/run-linux-commands-as-ruby-scripts/14453/1 "2004-10-28T12:55:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Bill\_Atkins](https://avatars.discourse-cdn.com/v4/letter/b/c4cdca/32.png) [@Bill\_Atkins](https://rubytalk.org/u/Bill_Atkins)\
**Post date:** [28 October 2004 13:16 UTC](https://rubytalk.org/t/run-linux-commands-as-ruby-scripts/14453/2 "2004-10-28T13:16:44Z")

</div>

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

> **···**
>
> On Thu, 28 Oct 2004 21:55:47 +0900, nkb \<nkb@pacific.net.sg\> wrote:
> 
> > 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.

---

<div class="post-metadata">

**Author:** ![I\_did\_not](https://avatars.discourse-cdn.com/v4/letter/i/22d042/32.png) [@I\_did\_not](https://rubytalk.org/u/I_did_not)\
**Post date:** [28 October 2004 14:09 UTC](https://rubytalk.org/t/run-linux-commands-as-ruby-scripts/14453/3 "2004-10-28T14:09:03Z")

</div>

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.

> **···**
>
> On Thu, 28 Oct 2004 21:55:47 +0900, nkb wrote:
> 
> > 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.
