• Login
sketchucation logo sketchucation
  • Login
ℹ️ GoFundMe | Our friend Gus Robatto needs some help in a challenging time Learn More

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

Scheduled Pinned Locked Moved Developers' Forum
4 Posts 3 Posters 4.0k Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    Aerilius
    last edited by 7 Mar 2011, 23:23

    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:

    @echo off
    set a="Hello World"
    ECHO %a%
    exit %a%
    
    1 Reply Last reply Reply Quote 0
    • D Offline
      Dan Rathbun
      last edited by 8 Mar 2011, 00:15

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

      I'm not here much anymore.

      1 Reply Last reply Reply Quote 0
      • D Offline
        Dan Rathbun
        last edited by 8 Mar 2011, 00:24

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

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • T Offline
          TIG Moderator
          last edited by 8 Mar 2011, 09:18

          This might be clearer
          ruby:

          x="E;/new_batch.cmd"
          UI.openURL(x)
          

          returns true
          batch file:

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

          TIG

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          1 / 1
          • First post
            1/4
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement