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.

···

--
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

···

-----Original Message-----
From: list-bounce@example.com
[mailto:list-bounce@example.com] On Behalf Of Pieter Jongsma
Sent: Sunday, September 09, 2007 6:57 AM
To: ruby-talk ML
Subject: 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.
--
Posted via http://www.ruby-forum.com/\.

Pieter Jongsma 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.

···

--
RMagick OS X Installer [http://rubyforge.org/projects/rmagick/\]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?forum_id=1618\]
RMagick Installation FAQ [http://rmagick.rubyforge.org/install-faq.html\]

Pieter Jongsma 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,

···

--
Jonas Roberto de Goes Filho (sysdebug)
http://goes.eti.br

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

Thank you!

···

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

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:

--
    
Email and shopping with the feelgood factor!
55% of income to good causes. http://www.ippimail.com

Quoth Terry Poulin 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,

···

--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertillnoon.com/

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 Meyer <konrad@tylerc.org>:

Quoth Terry Poulin 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,
--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertillnoon.com/

--
fish can't fly but birds can
birds can't swim but fish can
(exception penguins )