sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

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

    Scheduled Pinned Locked Moved Developers' Forum
    4 Posts 3 Posters 4.0k Views 3 Watching
    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

      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
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        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
        • Dan RathbunD Offline
          Dan Rathbun
          last edited by

          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
          • TIGT Offline
            TIG Moderator
            last edited by

            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
            • First post
              Last post
            Buy SketchPlus
            Buy SUbD
            Buy WrapR
            Buy eBook
            Buy Modelur
            Buy Vertex Tools
            Buy SketchCuisine
            Buy FormFonts

            Advertisement