Ruby <-> cmd command line: How to get return value?
-
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 truebatch file:
@echo off set a="Hello World" ECHO %a% exit %a% -
In the console... you can use backquoted strings to run shell commands.
**dir**An aleternate to using backquotes, is to use
%xdilimiters. 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.
~ -
Also I see you are using Environment variables.
The set of Environment variables is accessed from Ruby via the
ENVhash.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``
~ -
This might be clearer
ruby:x="E;/new_batch.cmd" UI.openURL(x)returns
true
batch file:@echo off echo "Hello World" echo pause exitWaits 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 ?
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement