Comments on: 6 Ways to Run Shell Commands in Ruby http://pasadenarb.com/2007/03/ruby-shell-commands.html The Pasadena Ruby Brigade Wed, 10 Mar 2010 23:48:54 +0000 http://wordpress.org/?v=2.1.2 By: Ilya http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-2 Ilya Wed, 14 Mar 2007 08:58:00 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-2 Thank you! Nice review. Thank you! Nice review.

]]>
By: Wayne E. Seguin http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-32 Wayne E. Seguin Thu, 05 Jul 2007 22:07:23 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-32 Don't forget the <code>%x{whoami}<code> which is the delimited form of the backquote <code>`whoami`</code>. Don’t forget the %x{whoami} which is the delimited form of the backquote `whoami`.

]]>
By: Marius http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-131 Marius Fri, 07 Sep 2007 11:11:37 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-131 Hey! Thanks for the overview! Do you happen to know how I can pass a ruby variable to the shell command that needs to be executed? Something like this: filename = 'test.tar' result = `tar xfv` + filename Of course this does not work, but how do I make it work ? ;) I have been googling for 30 minutes now and I can't find anything. Greetings from Bonn (DE) Hey! Thanks for the overview! Do you happen to know how I can pass a ruby variable to the shell command that needs to be executed? Something like this:

filename = ‘test.tar’ result = `tar xfv` + filename

Of course this does not work, but how do I make it work ? ;) I have been googling for 30 minutes now and I can’t find anything. Greetings from Bonn (DE)

]]>
By: Michael http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-156 Michael Sat, 15 Sep 2007 00:18:40 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-156 Marius - you can use exit_code = system("tar xfv #{filename}") Make sure to use double quotes, not single quotes. If you need to capture output, use Open3.popen with the same double-quoted string. Marius - you can use

exit_code = system(”tar xfv #{filename}”)

Make sure to use double quotes, not single quotes.

If you need to capture output, use Open3.popen with the same double-quoted string.

]]>
By: Jason Campbell http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-178 Jason Campbell Sat, 22 Sep 2007 07:50:30 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-178 Thanks! Seriously. I found this after almost an hour of scouring google. This is just what I needed! I added a line to Simon Gate's <a href="http://simongate.com/2007/6/5/basecamp-post-commit-hook" rel="nofollow">Basecamp post-commit hook</a> to automatically update a working copy of my repository using the backtick method. I saw the method before but since I am a noob to ruby I didn't quite get it until I found your article. Thanks again! Thanks! Seriously. I found this after almost an hour of scouring google. This is just what I needed!

I added a line to Simon Gate’s Basecamp post-commit hook to automatically update a working copy of my repository using the backtick method. I saw the method before but since I am a noob to ruby I didn’t quite get it until I found your article. Thanks again!

]]>
By: Floyd http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-194 Floyd Fri, 28 Sep 2007 02:41:58 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-194 Nice summary. It was helpful. Nice summary. It was helpful.

]]>
By: Garrett Smith http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-239 Garrett Smith Thu, 18 Oct 2007 17:46:13 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-239 Good write-up, but you forgot to mention that a few of these methods don't work on certian OSes, such as Windows. Good write-up, but you forgot to mention that a few of these methods don’t work on certian OSes, such as Windows.

]]>
By: james http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-250 james Sun, 21 Oct 2007 11:45:51 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-250 Very useful - Answered a lot of key questions Very useful - Answered a lot of key questions

]]>
By: JeanW http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-1215 JeanW Thu, 14 Feb 2008 20:12:30 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-1215 Great article! We actually ended up going with a simple solution to get our stderr on Windows-- we just redirected stderr to stdout inside the backticsk, e.g. `#{command} #{args} 2>&1` No need to install a new gem on every machine! Great article! We actually ended up going with a simple solution to get our stderr on Windows– we just redirected stderr to stdout inside the backticsk, e.g.

`#{command} #{args} 2>&1`

No need to install a new gem on every machine!

]]>
By: Murilo http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-1480 Murilo Thu, 28 Feb 2008 01:54:52 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-1480 Good tutorial. You just forgot to metion that I need to do require "open3" at the beginning of Section "Open3"; you only did it at the third block of code. I also couldn't use open4 on Debian, even after doing gem instal open4, does someone know the reason? (I don't have any experience on Ruby programming, just found the article and tried it out) Good tutorial. You just forgot to metion that I need to do require “open3″ at the beginning of Section “Open3″; you only did it at the third block of code. I also couldn’t use open4 on Debian, even after doing gem instal open4, does someone know the reason? (I don’t have any experience on Ruby programming, just found the article and tried it out)

]]>
By: links for 2008-03-11 « Bloggitation http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-1676 links for 2008-03-11 « Bloggitation Tue, 11 Mar 2008 00:19:22 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-1676 [...] 6 Ways to Run Shell Commands in Ruby (tags: ruby programming) [...] […] 6 Ways to Run Shell Commands in Ruby (tags: ruby programming) […]

]]>
By: IceskYsl http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-1885 IceskYsl Tue, 08 Apr 2008 15:27:47 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-1885 I notice a error like this: Code: logger.info("rar a #{local_dir_name} #{local_dir_name}") oo= %x{rar a #{local_dir_name} #{local_dir_name}} logger.info("oo=#{oo}") Output: rar a 1207666290_2 1207666290_2 oo= Program aborted can anybody give me a hand? I notice a error like this:

Code: logger.info(”rar a #{local_dir_name} #{local_dir_name}”) oo= %x{rar a #{local_dir_name} #{local_dir_name}} logger.info(”oo=#{oo}”)

Output: rar a 1207666290_2 1207666290_2 oo= Program aborted

can anybody give me a hand?

]]>
By: IceskYsl http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-1886 IceskYsl Tue, 08 Apr 2008 15:30:29 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-1886 and in my localhost,it's ok,output like this: ------------ rar a 1207667932_2 1207667932_2 oo= RAR 3.70 beta 1 Copyright (c) 1993-2007 Alexander Roshal 8 Jan 2007 Shareware version Type RAR -? for help Evaluation copy. Please register. Creating archive 1207667932_2.rar Adding 1207667932_2/datas.csv OK Adding 1207667932_2/images OK Adding 1207667932_2 OK Done -------- and in my localhost,it’s ok,output like this:

———— rar a 1207667932_2 1207667932_2 oo= RAR 3.70 beta 1 Copyright (c) 1993-2007 Alexander Roshal 8 Jan 2007 Shareware version Type RAR -? for help

Evaluation copy. Please register.

Creating archive 1207667932_2.rar

Adding 1207667932_2/datas.csv OK Adding 1207667932_2/images OK Adding 1207667932_2 OK Done ——–

]]>
By: Max http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-1924 Max Fri, 11 Apr 2008 16:13:29 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-1924 I'm trying to do what Marius is doing, but executing "tainted" input instead. Does anyone know if there is a way to escape shell arguments so they're safe? Also, you can install the win32-open3 gem to get this to work on Windows, btw. Oh, and nice article :) I’m trying to do what Marius is doing, but executing “tainted” input instead. Does anyone know if there is a way to escape shell arguments so they’re safe?

Also, you can install the win32-open3 gem to get this to work on Windows, btw.

Oh, and nice article :)

]]>
By: daniel rails http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-1938 daniel rails Mon, 14 Apr 2008 07:59:49 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-1938 Hey Nate, thanks for article, this was exactly what I was searching for! Greets, Daniel Hey Nate, thanks for article, this was exactly what I was searching for! Greets, Daniel

]]>
By: kingtutt http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-1981 kingtutt Sun, 20 Apr 2008 12:38:07 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-1981 You've restored my faith! And inspired me to use ruby for in place of bash scripting for system administration tasks...and that's just awesome. You’ve restored my faith! And inspired me to use ruby for in place of bash scripting for system administration tasks…and that’s just awesome.

]]>
By: wdso http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-2742 wdso Thu, 24 Jul 2008 23:12:06 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-2742 Brilliant summary. One question though… what about threads? Brilliant summary. One question though… what about threads?

]]>
By: Ben http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-11460 Ben Sat, 27 Dec 2008 21:39:01 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-11460 Note that Process::Status#to_i doesn't actually give you the exit status (rather, it gives you _all_ the bits). According to the RDoc "Posix systems record information on processes using a 16-bit integer. The lower bits record the process status (stopped, exited, signaled) and the upper bits possibly contain additional information (for example the program‘s return code in the case of exited processes)" If you want the exit status, you need to use Process::Status#exitstatus. To see the problem, note that the exit status of 'false' on is 1 (you can see this in the shell). But calling status.to_i gives you 256, not 1. However, calling status.exitstatus give you 1, as expected. Note that Process::Status#to_i doesn’t actually give you the exit status (rather, it gives you _all_ the bits). According to the RDoc

“Posix systems record information on processes using a 16-bit integer. The lower bits record the process status (stopped, exited, signaled) and the upper bits possibly contain additional information (for example the program‘s return code in the case of exited processes)”

If you want the exit status, you need to use Process::Status#exitstatus. To see the problem, note that the exit status of ‘false’ on is 1 (you can see this in the shell). But calling status.to_i gives you 256, not 1. However, calling status.exitstatus give you 1, as expected.

]]>
By: Ruby Shell Skripte http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-15951 Ruby Shell Skripte Sat, 31 Jan 2009 11:16:39 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-15951 [...] Ruby macht es einem dabei sehr einfach externe Skripte und Programme auszuführen. Es gibt allerdings verschiedene Möglichkeiten die in diesem Artikel beschrieben sind: 6 Ways to Run Shell Commands in Ruby [...] […] Ruby macht es einem dabei sehr einfach externe Skripte und Programme auszuführen. Es gibt allerdings verschiedene Möglichkeiten die in diesem Artikel beschrieben sind: 6 Ways to Run Shell Commands in Ruby […]

]]>
By: Aladdin With A Lamp » 『Ruby on Rails』|很好很好的资料! http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-17547 Aladdin With A Lamp » 『Ruby on Rails』|很好很好的资料! Thu, 12 Feb 2009 16:50:06 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-17547 [...] 6 Ways to Run Shell Commands in Ruby [...] […] 6 Ways to Run Shell Commands in Ruby […]

]]>
By: Andrew Grimm http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-21221 Andrew Grimm Mon, 06 Apr 2009 07:11:55 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-21221 I was trying to work out how to execute a command, get the exit status and ignore the textual output (for an integration test of my code), and the $? after backticks worked for me. Thanks! I was trying to work out how to execute a command, get the exit status and ignore the textual output (for an integration test of my code), and the $? after backticks worked for me. Thanks!

]]>
By: Inoe http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-21378 Inoe Fri, 10 Apr 2009 04:23:37 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-21378 thanks its very usefull.... nice post.. thanks its very usefull…. nice post..

]]>
By: Les liens du 24 04 2008 au 25 04 2008 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-21425 Les liens du 24 04 2008 au 25 04 2008 Fri, 10 Apr 2009 20:32:12 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-21425 [...] Pasadena.rb» Blog Archive » 6 Ways to Run Shell Commands in Ruby [...] […] Pasadena.rb» Blog Archive » 6 Ways to Run Shell Commands in Ruby […]

]]>
By: mz2guild's me2DAY http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-21777 mz2guild's me2DAY Tue, 28 Apr 2009 10:55:40 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-21777 <strong>mz2guild의 생각...</strong> 6 Ways to Run Shell Commands in Ruby... mz2guild의 생각…

6 Ways to Run Shell Commands in Ruby…

]]>
By: emil tin http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-23902 emil tin Mon, 18 May 2009 10:45:27 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-23902 in ruby 1.9, open3 provides a way to get the exit status. http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/22888 in ruby 1.9, open3 provides a way to get the exit status. http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/22888

]]>
By: links for 2009-12-22 « sySolution http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-26408 links for 2009-12-22 « sySolution Tue, 22 Dec 2009 15:01:28 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-26408 [...] Pasadena.rb» Blog Archive » 6 Ways to Run Shell Commands in Ruby (tags: shell ruby rubyonrails) [...] […] Pasadena.rb» Blog Archive » 6 Ways to Run Shell Commands in Ruby (tags: shell ruby rubyonrails) […]

]]>
By: myself http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-26434 myself Wed, 06 Jan 2010 18:19:02 +0000 http://pasadenarb.com/2007/03/ruby-shell-commands.html#comment-26434 great! precise , clear and helpful explanation great! precise , clear and helpful explanation

]]>