System Command not passing output to a value

Hello,

I’m trying to write a script that will take the output of a system
command and assign it to a variable whether it be a string, or array for
later processing. The following block:

irb(main):002:0> location = ‘C:\python\python332’
=> “C:\python\python332”
irb(main):003:0> location = location.gsub(’\’, ‘/’)
=> “C:/python/python332”
irb(main):004:0> value = #{"#{location}/python -V"}
Python 3.3.2
=> “”
irb(main):005:0> puts value

=> nil
irb(main):006:0> value = #{"ruby -v"}
=> “ruby 1.9.3p448 (2013-06-27) [i386-mingw32]\n”
irb(main):007:0> puts value
ruby 1.9.3p448 (2013-06-27) [i386-mingw32]
=> nil

is what I am receiving. ruby -v works, yet python -V does not, and
assigns nil. What would I be doing wrong in this instance?

On Thu, Dec 5, 2013 at 6:12 PM, Philip D. [email protected] wrote:

irb(main):004:0> value = #{"#{location}/python -V"}
You cannot nest string interpolation. Try

value = "#{location}/python" -V

Kind regards

robert

From the IRB responses, it looks like the command “python -V” is
printing the output and returning an empty string, I don’t think there’s
anything wrong with your code, it’s how “python -V” responds.

Of course, someone else may have a better understanding of this.

Edit: Nice catch as usual, Robert! I thought that command was getting
through, since you can see the response given: “Python 3.3.2”

On 12/05/2013 09:20 AM, Robert K. wrote:

=> “C:/python/python332”

Certainly you can nest string interpolation, although it’s silly in this
case (and most cases I’ve seen):

2.0.0p247 :001 > command = “ruby”
=> “ruby”
2.0.0p247 :002 > #{"#{command} -v"}
=> “ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]\n”

In any case, OP’s issue is that “ruby -v” outputs to stdout, and “python
-V” outputs to stderr which is not captured by backticks:

$ ruby -v > /dev/null
$ python -V > /dev/null
Python 2.7.5
$ python -V &> /dev/null
$

A simple way to deal with this would be to use Open3:

$ irb
2.0.0p247 :001 > require ‘open3’
=> true
2.0.0p247 :002 > Open3.capture2e(“ruby -v”)
=> [“ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]\n”,
#<Process::Status: pid 16154 exit 0>]
2.0.0p247 :003 > Open3.capture2e(“python -V”)
=> [“Python 2.7.5\n”, #<Process::Status: pid 16167 exit 0>]

-Justin

On 12/05/2013 11:17 AM, Philip D. wrote:

Thank you Justin, yes, I use the nested interpolation to help with the
commands and save code, but I will look into the Open3 and see what I
can do with that. Thank you for the help. I’ll post back once I figure
out what I need to do.

Just to be totally explicit, these are equivalent:

#{"ruby -v"} is the same as ruby -v
#{"#{location}/python -V"} is the same as #{location}/python -V

-Justin

Joel P. wrote in post #1129747:

From the IRB responses, it looks like the command “python -V” is
printing the output and returning an empty string, I don’t think there’s
anything wrong with your code, it’s how “python -V” responds.

Of course, someone else may have a better understanding of this.

Edit: Nice catch as usual, Robert! I thought that command was getting
through, since you can see the response given: “Python 3.3.2”

Thank you for that information, it definitely sheds some light on that
whole bit. I wouldn’t have known what to search for to find that out.

Justin C. wrote in post #1129754:

On 12/05/2013 09:20 AM, Robert K. wrote:

=> “C:/python/python332”

Certainly you can nest string interpolation, although it’s silly in this
case (and most cases I’ve seen):

2.0.0p247 :001 > command = “ruby”
=> “ruby”
2.0.0p247 :002 > #{"#{command} -v"}
=> “ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]\n”

In any case, OP’s issue is that “ruby -v” outputs to stdout, and “python
-V” outputs to stderr which is not captured by backticks:

$ ruby -v > /dev/null
$ python -V > /dev/null
Python 2.7.5
$ python -V &> /dev/null
$

A simple way to deal with this would be to use Open3:

$ irb
2.0.0p247 :001 > require ‘open3’
=> true
2.0.0p247 :002 > Open3.capture2e(“ruby -v”)
=> [“ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]\n”,
#<Process::Status: pid 16154 exit 0>]
2.0.0p247 :003 > Open3.capture2e(“python -V”)
=> [“Python 2.7.5\n”, #<Process::Status: pid 16167 exit 0>]

-Justin

Thank you Justin, yes, I use the nested interpolation to help with the
commands and save code, but I will look into the Open3 and see what I
can do with that. Thank you for the help. I’ll post back once I figure
out what I need to do. I’m also on Windows, I’m not sure how I’d test
the StdOut, and StdErr with Command Prompt.

On Thu, Dec 5, 2013 at 7:54 PM, Justin C. [email protected]
wrote:

On 12/05/2013 09:20 AM, Robert K. wrote:

On Thu, Dec 5, 2013 at 6:12 PM, Philip D. [email protected] wrote:

You cannot nest string interpolation. Try

Certainly you can nest string interpolation, although it’s silly in this case
(and most cases I’ve seen):

Thank you for correcting me! I was too quickly glancing over the
expression and apparently overlooked the double quotes. And, yes, I
agree: if nesting is necessary than string interpolation is probably
not the best approach.

Cheers

robert