[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Run UNIX command

Pieter Jongsma

9/9/2007 1:57:00 PM

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

5 Answers

Tim Hunter

9/9/2007 2:29:00 PM

0

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/project...]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?for...]
RMagick Installation FAQ [http://rmagick.rubyforge.org/instal...]


Pieter Jongsma

9/9/2007 2:59:00 PM

0

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


Thank you!
--
Posted via http://www.ruby-....

Terry Poulin

9/11/2007 4:18:00 AM

0

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.

--
http://en.wikipedia.org/wiki/FORTRAN#FORTRAN_...
Note the listing about 'pi' ^_^


--

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


Konrad Meyer

9/11/2007 4:48:00 AM

0

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? :P

Cheers,
--
Konrad Meyer <konrad@tylerc.org> http://konrad.sobertil...

Jonas Roberto de Goes Filho (sysdebug)

9/11/2007 7:53:00 PM

0

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://g...