0

I'm trying to get the return value of a ruby script in cmd.exe. The ruby script will return -1 if exception is caught and 0 if success.

rescue Exception => ex
  puts ex.message
  returnvalue = -1
else
  returnvalue = 0
ensure
  puts returnvalue

in the cmd batch, calling the script using ruby.exe

ruby tt.rb 1 %1

But I'm not able to retreive the returnvalue in the cmd batch file (e.g. using %errorlevel%). Is there any way to do this?

thanks!

amw_jjj
  • 3
  • 1
  • Do not exit with a negative value. Exit with 0 on success and a value greater 0 on error. Microsoft strongly recommends not returning a negative value. Also on Linux/Unix no negative value should be returned on exit by an application or script for various reasons. – Mofi Aug 25 '14 at 17:37

1 Answers1

1

puts -1 does not make ruby.exe return -1; it makes it print -1. It returns -1 if you do exit -1.

In *nix, it is easy to capture the output (as opposed to the exit code) of the program into a variable. Under Windows, not so easy; there is a trick you can do involving the for construct, but it is of limited use.

Community
  • 1
  • 1
Amadan
  • 191,408
  • 23
  • 240
  • 301