Run UNIX command

Hi,

I’m looking for a way to execute an UNIX command from a ruby program.
For instance, when the user selected a .tar.gz file, using the untar
command to decompress it.
How can I do that?

Thanks in advance!
pieter.

For instance, when the user selected a .tar.gz file, using the untar
command to decompress it.
How can I do that?

Thanks in advance!
pieter.

Posted via http://www.ruby-forum.com/.

There are several ways to make system calls:

$ irb
irb(main):001:0> exec “echo Hi”
Hi
$

exec replaces the current process with the given external command

$ irb
irb(main):001:0> system “echo Hi”
Hi
=> true

system runs the external command and returns true on success and false
on
failure

irb(main):002:0> echo Hi
=> “Hi\n”
irb(main):003:0> %x{ echo Hi }
=> “Hi\n”
irb(main):004:0>

backticks and the %x literal execute the external command and return its
output.

HTH,

Felix

$ irb
irb(main):001:0> system “echo Hi”
Hi
=> true

Thank you!

Pieter J. wrote:

Hi,

I’m looking for a way to execute an UNIX command from a ruby program.
For instance, when the user selected a .tar.gz file, using the untar
command to decompress it.
How can I do that?

Thanks in advance!
pieter.

There’s a variety of ways to do that, depending on your needs. The
simplest is to use the Kernel.system method:

system(“ls”)

If you need the stdout output from the command, use backticks:

output = ls

If you need more control, check out IO.popen.

Since the question has been answered, I’d like to pipe in my own two
cents.

Remember what you pass to system() will not work as expected if the
system
running it does not have it. For example, if you use an option specific
to a
GNU Program that is not supported by a given POSIX system, it won’t work
right. If you use system( ‘gedit’ ) and the user does not have gedit in
their
path, well joy for your program and users.

Ruby has a large standard library that can do much of what the basic
UNIX and
DOS commands can do. You should really take a look at FileUtils or
ftools in
ri before you decide to do some thing that will only work on a specific
system
family (e.g. GNU, BSD, POSIX, DOS). Other wise, why bother with a
language
that provides such things out of the box… It’ll just create more work
later
if you ever want to use your Program on say, an OS/2 or Windows machine.

system() is better for things that are just wasteful to write yourself
or to
much bother to do yourself in my humble opinion.

An example of using command specific to GNU’s tar program with the tar
implementation on my OpenBSD system.

Terry@vectra-$ touch foo bar
Terry@vectra-$ tar -cf test1.tar foo bar
Terry@vectra-$ ls
bar foo test1.tar
Terry@vectra-$ tar --create --file=test2.tar foo bar
tar: unknown option – -
usage: tar {crtux}[014578befHhLmOoPpqsvwXZz]
[blocking-factor | archive | replstr] [-C directory] [-I file]
[file …]
tar {-crtux} [-014578eHhLmOoPpqvwXZz] [-b blocking-factor]
[-C directory] [-f archive] [-I file] [-s replstr] [file …]

TerryP.

Note the listing about ‘pi’ :slight_smile:

Quoth Terry P. on Monday 10 September 2007 09:17:54 pm:

if you ever want to use your Program on say, an OS/2 or Windows machine
… then you’re a masochist? Who uses OS/2 anymore? :stuck_out_tongue:

Cheers,

in terminal on a mac you just open it and type
ruby filename.rb and it will run it
(does not work in irb mode)

2007/9/10, Konrad M. [email protected]:

Pieter J. wrote:

Hi,

I’m looking for a way to execute an UNIX command from a ruby program.
For instance, when the user selected a .tar.gz file, using the untar
command to decompress it.
How can I do that?

Thanks in advance!
pieter.

Use ``. An example:

irb(main):042:0> date
=> “Ter Set 11 16:50:30 BRT 2007\n”

Regards,