ruby <-> cmd command line: How to get return value?

ruby <-> cmd command line: How to get return value?

Postby Aerilius » Tue Mar 08, 2011 12:23 am

Hello,
I'm just beginning with ruby and have not much practice with it. My aim is to launch a command on Windows cmd (actually later a bash shell) and get a return value into the ruby console. I have found how to launch external programs or for example a batch file, however I didn't find if it's possible to receive a result from the batch script.
Is there another command or parameter necessary in ruby or do I have an error in the batch script (exit %a%)?

ruby:
x="E:/new_batch.bat"
UI.openURL(x)

returns only true

batch file:
Code: Select all
@echo off
set a="Hello World"
ECHO %a%
exit %a%
0

Aerilius 
PluginStore Author
PluginStore Author
 

Re: ruby <-> cmd command line: How to get return value?

Postby Dan Rathbun » Tue Mar 08, 2011 1:15 am

In the console... you can use backquoted strings to run shell commands.
`dir`

An aleternate to using backquotes, is to use %x dilimiters. The delimiter character is your choice, or you can choose one of the bracket sets ( ), [ ] or { }, ex:
listing = %x[dir]

So a backquoted shell string will return the STDOUT from the shell command (in the example above I store it in the string reference listing.)


And, before you ask, there's a bug / annoyance in Ruby where we can't suppress having the shell window pop up for a half-second or so.
~
0
    I'm not here much anymore. But a PM will fire email notifications.
    User avatar
    Dan Rathbun 
    PluginStore Author
    PluginStore Author
     

    Re: ruby <-> cmd command line: How to get return value?

    Postby Dan Rathbun » Tue Mar 08, 2011 1:24 am

    Also I see you are using Environment variables.

    The set of Environment variables is accessed from Ruby via the ENV hash.

    So on my system:
    ENV['USERNAME']
    >> Dan

    There are differences between MAC and PC... on a MAC it would be:
    ENV['USER']

    To list the Enviroment variables from Ruby, you can do (cross-platform):
    ENV.each {|k,v| puts "#{k} = #{v}" }
    but it's simpler, to do (for PC):
    `set`
    ~
    0
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: ruby <-> cmd command line: How to get return value?

      Postby TIG » Tue Mar 08, 2011 10:18 am

      This might be clearer
      ruby:
      Code: Select all
      x="E:/new_batch.cmd"
      UI.openURL(x)
      returns true
      batch file:
      Code: Select all
      @echo off
      echo "Hello World"
      echo
      pause
      exit
      Waits for you to close it...
      x="file:/E:/new_batch.cmd"
      will also work...
      Writing the bat or cmd file to the root directory would probably be better if it were into a Temp folder ?
      0
      TIG
      User avatar
      TIG 
      Global Moderator
       

      SketchUcation One-Liner Adverts

      by Ad Machine » 5 minutes ago



      Ad Machine 
      Robot
       



       

      Return to Developers' Forum

      Who is online

      Users browsing this forum: patammar and 5 guests

      Visit our sponsors: